简体   繁体   中英

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"
  }
]

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);

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);

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)

Logic

  • Loop through first array
  • Find the matching items from second array by comparing the addrSeq values.
  • Update the vpainfo of item in first array as the concatenated value of vpa s from second array.

Working Fiddle

 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" } ] arr1.forEach((node) => node.vpainfo = node.vpainfo.concat(arr2.filter(item => item.addrSeq === node.addrSeq).map(item => item.vpa))) console.log(arr1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM