I have 2 arrays of objects. There are duplicates between the arrays. I want to merge them into 1 array of all unique objects (so no duplicates).
How can I do this by comparing the "id" of each object?
Underscore.js has a method called _.uniq()
. It looks to be correct, but I can't get the syntax for the "iterator" argument correct.
var firstArray = [{ id: 1, name: "foo"}, { id: 2, name: "bar" }];
var secondArray = [{ id: 2, name: "boop" }, { id: 3, name: "baz" }];
firstArray.push(secondArray);
var myUniqueArray = _.uniq(firstArray, false, ???);
myUniqueArray // [{ id: 1, name: "foo"}, { id: 2, name: "bar" }, { id: 3, name: "baz" }];