Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!