简体   繁体   English

如何合并查找差异数组和数组json JavaScript?

[英]how to merge find difference array and array json javascript?

Anyone can help me with merge difference two array and array json . 任何人都可以帮助我合并两个数组数组json的区别。 You can check my below code and result that I want to check difference between arr and arr2 您可以检查我下面的代码和结果,我想检查arrarr2之间的区别

First Array 第一阵列

let arr=[ '124', '125', '126', '127' ]

Second Array 第二阵列

let arr2=[{ 
            _id: '125',
            itemId: '125',
            onHand: 10,
            inventoryValue: 70,
            avgCost: 7 
          },
          { 
            _id: '124', 
            itemId: '124', 
            onHand: 10,
            inventoryValue: 50,
            avgCost: 5 
          } 
         ]

I want result like that 我想要这样的结果

let arr3=['126', '127' ]

Just filter your arr based on whether arr2 does not include each value as an _id by using some : 只需根据arr2是否不包含每个值作为_idfilter您的arr方法是使用some

 let arr = ['124', '125', '126', '127'] let arr2 = [{ _id: '125', itemId: '125', onHand: 10, inventoryValue: 70, avgCost: 7 }, { _id: '124', itemId: '124', onHand: 10, inventoryValue: 50, avgCost: 5 } ]; let arr3 = arr.filter(e => !arr2.some(({ _id }) => _id == e)); console.log(arr3); 

Find the index of all the ids od arr2 in arr1 and splice them form arr1 找到arr1中所有id od arr2的索引并将它们拼接成arr1

 let arr = ['124', '125', '126', '127']; let arr2 = [{ _id: '125', itemId: '125', onHand: 10, inventoryValue: 70, avgCost: 7 }, { _id: '124', itemId: '124', onHand: 10, inventoryValue: 50, avgCost: 5 } ]; arr2.forEach(function(element) { var index = arr.indexOf(element._id); if (index > -1) { arr.splice(index, 1); } }); console.log(arr); 

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

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