How to use underscore's “intersection” on objects?

后端 未结 10 1130
春和景丽
春和景丽 2020-12-02 15:49
_.intersection([], [])

only works with primitive types, right?

It doesn\'t work with objects. How can I make it work with objects (maybe b

10条回答
  •  一生所求
    2020-12-02 16:21

    Technically, it does work on objects, but you need to be careful of reference equality.

    var jake = {'id': 1, 'name': 'jake' },
        jenny = {'id':4, 'name': 'jenny'},
        nick =  {'id': 9, 'name': 'nick'};
    var a = [jake, jenny]
    var b = [jake, nick];
    
    _.intersection(a, b);
    // is
    [jake]
    

提交回复
热议问题