JavaScript get elements from an object array that are not in another

前端 未结 5 1891
南笙
南笙 2020-12-15 18:20

I\'m new in JavaScript programming and I have two object arrays that have the following structure:

myFirstObjArray = [{foo: 1, bar: 1}, {foo: 3, bar: 3}, {fo         


        
5条回答
  •  不思量自难忘°
    2020-12-15 18:46

    You could filter by look up.

    const unique = a => o => !a.some(({ foo }) => o.foo === foo);
    
    var first = [{foo: 1, bar: 1}, {foo: 3, bar: 3}, {foo: 4, bar: 5}],
        second = [{foo: 2}, {foo: 4}, {foo: 5}],
        uniqueFirst = first.filter(unique(second)),
        uniqueSecond = second.filter(unique(first));
        
    console.log(uniqueFirst);
    console.log(uniqueSecond);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

提交回复
热议问题