List1:
[1,2,3,4]
List2:
[{id:1,name:hi},{id:3,name:hi},{id:5,name:hi}]
How can I check which items from List1 is missing from List2?
List1:
[1,2,3,4]
List2:
[{id:1,name:hi},{id:3,name:hi},{id:5,name:hi}]
How can I check which items from List1 is missing from List2?
Share Improve this question edited Mar 29, 2016 at 15:22 JJJ 33.2k20 gold badges94 silver badges103 bronze badges asked Mar 29, 2016 at 15:21 iCodeLikeImDrunkiCodeLikeImDrunk 17.9k36 gold badges116 silver badges174 bronze badges 2- 3 There are many ways to do that. What have you tried? – Yaron Schwimmer Commented Mar 29, 2016 at 15:25
- i've tried nothing and i'm all outta ideas. sorry, im a java background, so still learning the ropes of js – iCodeLikeImDrunk Commented Mar 29, 2016 at 15:31
5 Answers
Reset to default 4You can do something like this with help of map()
and filter()
var list1 = [1, 2, 3, 4],
list2 = [{
id: 1,
name: 'hi'
}, {
id: 3,
name: 'hi'
}, {
id: 5,
name: 'hi'
}];
// get array of id's
var ids = list2.map(function(v) {
return v.id;
})
// get missing elements
var miss = list1.filter(function(v) {
// check element in id's array
return ids.indexOf(v) == -1;
});
document.write('<pre>' + JSON.stringify(miss, null, 3) + '</pre>');
Using ES6 arrow function
var list1 = [1, 2, 3, 4],
list2 = [{
id: 1,
name: 'hi'
}, {
id: 3,
name: 'hi'
}, {
id: 5,
name: 'hi'
}];
// get array of id's
var ids = list2.map(v => v.id);
// get missing elements
var miss = list1.filter(v => ids.indexOf(v) == -1);
document.write('<pre>' + JSON.stringify(miss, null, 3) + '</pre>');
Use Array.prototype.filter
var list1 = [1,2,3,4];
var list2 = [{id:1,name:'hi'},{id:3,name:'hi'},{id:5,name:'hi'}];
var t = list2.map(e => e.id); // get all 'ids' from 'list2'
var result = list1.filter(e => t.indexOf(e) == -1);
document.write(JSON.stringify(result));
I would try to reduce list2 to array of missing ids. Maybe like this:
var data = [{id: 1, name: 'hi'}, {id: 3, name: 'hi'}, {id: 5, name: 'hi'}]
var ids = [1, 2, 3, 4]
var missing = data.reduce(function(prev, curr) {
return prev.filter(function(id) { return id !== curr.id })
}, ids.slice())
document.write(missing)
A solution with linear plexity for sorted arrays.
var list1 = [1, 2, 4, 30],
list2 = [{ id: 1, name: 'hi' }, { id: 3, name: 'hi' }, { id: 5, name: 'hi' }],
missing = list1.filter(function (a) {
while (list2[this.index] && list2[this.index].id < a) {
this.index++;
}
return !list2[this.index] || list2[this.index].id !== a;
}, { index: 0 });
document.write('<pre>' + JSON.stringify(missing, 0, 4) + '</pre>');
Combine Array methods filter
and some
:
var list1 = [1,2,3,4];
var list2 = [{id:1,name:'hi'},{id:3,name:'hi'},{id:5,name:'hi'}];
return list2.filter(function(o) {
return !list1.some(function(id) { return o.id === id; })
})
which yields the objects from list2
whose ids are not in list1
.
The converse, as I saw many people post, is this:
return list1.filter(function(id) {
return !list2.some(function(o) { return o.id === id; });
});
which yields the ids from list1
that do not have corresponding objects in list2
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744372306a4571016.html
评论列表(0条)