javascript - Get the JSON objects that are not present in another array - Stack Overflow
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
5 Answers
Reset to default 6Use _.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);
- 2015年世界科技发展回顾:信息技术
- 思科宣布27亿美元收购安全软件厂商Sourcefire
- 仅售74美元的Android迷你电脑:你会买吗?
- react native - Handling loginlogout without user undefined errors - Stack Overflow
- TGUI Using raylib backend with cmake - Stack Overflow
- r markdown - Rstudio custom traceback - Stack Overflow
- ST_CONTAINS() giving FALSE even if Point lies within polygon. google-bigquery - Stack Overflow
- Banning telegram channels from groups as a bot - Stack Overflow
- maven - Quarkus Live Reload in nested directories - Stack Overflow
- c - Dereferencing a valid pointer results in an error - Stack Overflow
- android - How to reduce height of a composable to wrap content without padding - Stack Overflow
- swiftui - Cannot access generic subscript - Stack Overflow
- Flutter : Privacy screen implementation - Stack Overflow
- node.js - Attach .pfx certificate to nodejsaxios HTTP request - Stack Overflow
- javascript - Firebase Auth link - Problem with the Google login, no possibility to change to own project name - Stack Overflow
- React Native Build Error on Windows: "EMFILE: too many open files" During `assembleRelease` - Stack Overflow
- python - Problem clearing errorbar artists from dynamic matplotlib figure - Stack Overflow