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

后端 未结 8 790
温柔的废话
温柔的废话 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

    You just need to break the inner loop when a match is found:

    if (a[i].name == b[j].name) {
        b.splice(j, 1);
        break;
    }
    
    0 讨论(0)
  • 2020-12-06 02:26

    compare and remove in array of object.Typically array of object data type may be typeOf is object.So that we need to convert into JSON stringify and then check condition..

    for(var i=0; i < a.length; i++) {
                        for(var j=0; j < b.length; j++) {
                            if(JSON.stringify(a[i])  == JSON.stringify(b[j])) {
                                a.splice(i, 1);
                            }
                        }
                    }

    0 讨论(0)
提交回复
热议问题