Compare two arrays of objects and remove items in the second one that have the same property value

后端 未结 8 796
温柔的废话
温柔的废话 2020-12-06 01:55

All I need to do is compare two arrays of objects and remove items in the second one that have the same property value. For example:

var a = [{\'name\':\'bob         


        
8条回答
  •  既然无缘
    2020-12-06 02:16

    Instead of using two loops you might also use the findIndex function:

    for (var i = 0, len = a.length; i < len; i++) {
        var ItemIndex = b.findIndex(b => b.name === a[i].name);
    
        a.splice(ItemIndex, 1)
    }
    

    Or if you want to go completely without using a loop you might use the forEach function

    a.forEach(function(item, index, array) {
        var ItemIndex = b.findIndex(b => b.name === item.name);
    
        a.splice(ItemIndex, 1)
    }
    

提交回复
热议问题