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

后端 未结 10 1139
春和景丽
春和景丽 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:20

    In lodash 4.0.0. We can try like this

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

    Output:

    [ {'id': 1, 'name': 'jake' } ];

提交回复
热议问题