简体   繁体   English

JavaScript 比较两个 arrays 并将在第二个数组中具有相同属性值的项目推送到第一个数组

[英]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.我有两个 arrays 并想从具有相同属性值的第二个数组中推送值。 Comparing the values by using addrSeq property and push the matching object in to vpainfo array使用addrSeq属性比较值并将匹配的 object 推送到vpainfo数组

Below are the sample JSON structure,以下是示例 JSON 结构,

Array1 :阵列 1

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

Array2:阵列 2:

[
  {
    "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.你只需要学习 javascript,这是结果请看这个我只是将两者都存储到数组中,然后运行循环并匹配并推入一个。

    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.您可以通过简单的forEach循环来实现它。

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.假设 OP 需要更新Array1以便每个对象的vpaIinfo数组根据Array2中的数据进行更新,以下解决方案可能是实现预期目标的一种可能方法。

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()使用.map()遍历 array-1
  • For each object, keep the rest of the props as-is & manipulate only vpainfo对于每个 object,保持道具的 rest 不变并仅操作vpainfo
  • Retain any existing values in current vpainfo (using ...obj.vpainfo )保留当前vpainfo中的任何现有值(使用...obj.vpainfo
  • Append to this, any relevant vpa found in array-2 Append 到此,array-2 中找到的任何相关vpa
  • 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)以上是通过使用.filter()只保留那些具有相同addrSeq然后,使用.map()只提取vpa道具(使用解构)来完成的

Logic逻辑

  • Loop through first array遍历第一个数组
  • Find the matching items from second array by comparing the addrSeq values.通过比较addrSeq值从第二个数组中找到匹配项。
  • Update the vpainfo of item in first array as the concatenated value of vpa s from second array.将第一个数组中 item 的vpainfo更新为第二个数组中vpa的连接值。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两个对象数组并删除第二个对象数组中具有相同属性值的项目 - Compare two arrays of objects and remove items in the second one that have the same property value Javascript比较两个不同大小的数组,并返回不在第二个数组中的项目 - Javascript Compare two arrays with different sizes and return the items that are not in second array Javascript-比较两个数组并将缺少的项添加到第一个数组中 - Javascript - Compare two arrays and add missing items into first array 推送到数组并比较两个数组的JavaScript - Push to array and compare two arrays JavaScript 比较两个arrays的对象,判断第一个数组的日期是否在第二个数组Javascript的指定范围内 - Compare two arrays of objects and find out if the date from the first array is within the specified range in the second array in Javascript 比较两个 arrays 并将相同的元素推送到一个数组中,将 rest 推送到另一个数组中的 javascript - Compare two arrays and push same element in one array and rest in other array in javascript 我有两个 arrays,第一个数组中的值与第二个数组的 ID 字段中的值相同 - I have two arrays, with the same values in the first array as in the ID fields of the second array 比较两个数组并更新Javascript中的第二个数组元素 - Compare two arrays and update second array element in Javascript 如何比较两个对象数组,如果值相同,则添加新键:Javascript 中第二个对象的值 - How to compare two array of object and if value are same add new key: value on second object in Javascript 将值推入JavaScript中的数组数组 - Push value into array of arrays in JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM