All I have to do is compare two arrays of objects and remove the elements in the second that have the same property value. For example:
var a = [{'name':'bob', 'age':22}, {'name':'alice', 'age':12}, {'name':'mike', 'age':13}]; var b = [{'name':'bob', 'age':62}, {'name':'kevin', 'age':32}, {'name':'alice', 'age':32}]; function remove_duplicates(a, b) { for (var i = 0, len = a.length; i < len; i++) { for (var j = 0, len = b.length; j < len; j++) { if (a[i].name == b[j].name) { b.splice(j, 1); } } } console.log(a); console.log(b); } console.log(a); console.log(b); remove_duplicates(a,b);
I do not understand why this does not work and instead gives:
Uncaught TypeError: Cannot read property 'name' of undefined
What I expected was the following content in b:
[{'name':'kevin', 'age':32}];
javascript object arrays
raben
source share