search

Home  >  Q&A  >  body text

javascript - How to use loadash to compare arrays of the form [object, object, object]

Array a

var a = [{key:1,name:'name1'},{key:2,name:'name2'},{key:3,name:'name3'},{key:4,name:'name4'},{key:5,name:'name5'}];

When array b is var b = [{key:1,name:'name1'}];, the final result should be [{key:2,name:'name2' },{key:3,name:'name3'},{key:4,name:'name4'},{key:5,name:'name5'}]

When array b is var b = [{key:3,name:'name3'},{key:4,name:'name4'}];, the final result should be [{key:1,name:'name1'},{key:2,name:'name2'},{key:5,name:'name5'}]

That is, I want to get the result of array a-array b.

Using difference is only effective for arrays, not for objects inside it

var a2 = [1,2,3,4,5];
        var b2 = [1,5];
        console.log(_.difference(a2,b2))
我想大声告诉你我想大声告诉你2761 days ago755

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-06-28 09:30:33

    _.differenceWith(a, b, _.isEqual)

    reply
    0
  • Cancelreply