Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
JavaScript compare two arrays and push items to the first array that have the same property value in the second array

I have two arrays and want to push the values from the second array having the same property value. Comparing the values by using addrSeq property and push the matching object in to vpainfo array

Below are the sample JSON structure,

Array1:

[
  {
    "addrSeq": "12",
    "accountMask": "********7479",
    "vpainfo": []
  },
  {
    "addrSeq": "13",
    "accountMask": "********74",
    "vpainfo": []
  }
]

Array2:

[
  {
    "addrSeq": "12",
    "vpa": "saasasa@fff"
  },
  {
    "addrSeq": "12",
    "vpa": "xyz@com"
  },
  {
    "addrSeq": "13",
    "vpa": "saas@ddd"
  }
]
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

you just need to learn javascript, here is result please look into this i just store both into array, and then run loop and match and push into one.

    let firstarray =[
  {
    "addrSeq": "12",
    "accountMask": "********7479",
    "vpainfo": []
  },
  {
    "addrSeq": "13",
    "accountMask": "********74",
    "vpainfo": []
  }
]

let secondarray = [
  {
    "addrSeq": "12",
    "vpa": "saasasa@fff"
  },
  {
    "addrSeq": "12",
    "vpa": "xyz@com"
  },
  {
    "addrSeq": "13",
    "vpa": "saas@ddd"
  }
]
// merge both array into one using addrSeq match value 
let mergedarray = firstarray.map(function(item) {
    secondarray.map(function(item2) {
        if (item.addrSeq == item2.addrSeq) {
            item.vpainfo.push(item2);
        }
    });
  return item;
});

console.log(mergedarray);
about 4 years ago · Juan Pablo Isaza Relatório

0

You can achieve it via simple forEach loop.

Demo :

const array1 = [
  {
    "addrSeq": "12",
    "accountMask": "********7479",
    "vpainfo": []
  },
  {
    "addrSeq": "13",
    "accountMask": "********74",
    "vpainfo": []
  }
];

const array2 = [
  {
    "addrSeq": "12",
    "vpa": "saasasa@fff"
  },
  {
    "addrSeq": "12",
    "vpa": "xyz@com"
  },
  {
    "addrSeq": "13",
    "vpa": "saas@ddd"
  }
];

array2.forEach((obj) => {
    array1.forEach((array1Obj) => {
    if (obj.addrSeq === array1Obj.addrSeq) {
        array1Obj.vpainfo.push(obj.vpa)
    }
  });
});

console.log(array1);

about 4 years ago · Juan Pablo Isaza Relatório

0

Assuming that the OP requires Array1 to be updated such that each object's vpaIinfo array gets updated based on the data in Array2, the following solution may be one possible way to achieve the desired objective.

Code Snippet

const getUpdatedArr = (ar1, ar2) => (
  ar1.map(
    obj => ({
      ...obj,
      vpainfo: [
        ...obj.vpainfo,
        ...ar2.filter(
          ({addrSeq}) => (obj.addrSeq === addrSeq)
        ).map(
          ({vpa}) => vpa
        )
      ]
    })
  )
);

const arr1 = [
  {
    "addrSeq": "12",
    "accountMask": "********7479",
    "vpainfo": []
  },
  {
    "addrSeq": "13",
    "accountMask": "********74",
    "vpainfo": []
  }
];

const arr2 = [
  {
    "addrSeq": "12",
    "vpa": "saasasa@fff"
  },
  {
    "addrSeq": "12",
    "vpa": "xyz@com"
  },
  {
    "addrSeq": "13",
    "vpa": "saas@ddd"
  }
];

console.log(getUpdatedArr(arr1, arr2));

Explanation

  • Iterate over array-1 using .map()
  • For each object, keep the rest of the props as-is & manipulate only vpainfo
  • Retain any existing values in current vpainfo (using ...obj.vpainfo)
  • Append to this, any relevant vpa found in array-2
  • The above is accomplished by using .filter() to retain only those with same addrSeq -AND- then, using .map() to extract just the vpa prop (using de-structuring)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda