Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda