javascript - How to check if an id (inside a list of ids) exists in an array of objects with ids? - Stack Overflow

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 Lis

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
Add a ment  | 

5 Answers 5

Reset to default 4

You 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信