最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get the JSON objects that are not present in another array - Stack Overflow

programmeradmin5浏览0评论

I have two arrays.

array1 = [
 {'id':1},
 {'id': 2}
]

and

array2 = [
 {'idVal':1},
 {'idVal': 2},
 {'idVal': 3},
 {'idVal': 4}
]

I need a optimal way, lodash if possible so that i can pare these two arrays and get a result array that has object present in array2 and not in array1. The keys have different name in both arrays. So the result will be,

res = [
  {'idVal': 3},
  {'idVal': 4}
]

I have two arrays.

array1 = [
 {'id':1},
 {'id': 2}
]

and

array2 = [
 {'idVal':1},
 {'idVal': 2},
 {'idVal': 3},
 {'idVal': 4}
]

I need a optimal way, lodash if possible so that i can pare these two arrays and get a result array that has object present in array2 and not in array1. The keys have different name in both arrays. So the result will be,

res = [
  {'idVal': 3},
  {'idVal': 4}
]
Share Improve this question edited Dec 14, 2017 at 11:47 Ankit Agarwal asked Dec 14, 2017 at 11:41 Ankit AgarwalAnkit Agarwal 30.8k5 gold badges40 silver badges63 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 6

Use _.differenceWith() with a parator method. According to the docs about _.difference() (differenceWith is based on difference):

Creates an array of array values not included in the other given arrays using SameValueZero for equality parisons. The order and references of result values are determined by the first array.

So array2 should be the 1st param passed to the method.

var array1 = [
  {'id': 1},
  {'id': 2}
];

var array2 = [
  {'idVal': 1},
  {'idVal': 2},
  {'idVal': 3},
  {'idVal': 4}
];

var result = _.differenceWith(array2, array1, function(arrVal, othVal) {
  return arrVal.idVal === othVal.id;
});

console.log(result);
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Using ES6

const result = array2.filter(item => !array1.find(i => i.idVal === item.id))

var array1 = [
 {'id':1},
 {'id': 2},
 {'id': 3},
 {'id': 4}
]

var array2 = [
 {'id':1},
 {'id': 3},
 {'id': 4}
]

notInArray2 = array1.reduce( function(acc, v) {
  if(!array2.find(function (vInner) {
    return v.id === vInner.id;
  })){
    acc.push(v);
  }
  return acc
}, []);

console.log(JSON.stringify(notInArray2))

Here's an optimized solution, not using lodash though. I created a search index containing just the values of array1, so that you can look up elements in O(1), rather than going through the entire array1 for every element in array2.

Let m be the size of array1 and n be the size of array2. This solution will run in O(m+n), as opposed to O(m*n) that you would have without prior indexing.

const array1 = [
 {'id':1},
 {'id': 2}
];

const array2 = [
 {'idVal':1},
 {'idVal': 2},
 {'idVal': 3},
 {'idVal': 4}
];

const array1ValuesIndex = {};
array1.forEach(entry => array1ValuesIndex[entry.id] = true);

const result = array2.filter(entry => !array1ValuesIndex[entry.idVal]);
console.log(result);

array1 = [
 {'id':1},
 {'id': 2}
]

array2 = [
 {'idVal':1},
 {'idVal': 2},
 {'idVal': 3},
 {'idVal': 4}
]

var array1Keys=array1.map(function(d1){ return d1.id});

var result =array2.filter(function(d){ return array1Keys.indexOf(d.idVal)==-1 })
console.log(result);
发布评论

评论列表(0)

  1. 暂无评论