javascript - Underscore.js get an array of unique objects -


i have 2 arrays of objects. there duplicates between arrays. want merge them 1 array of unique objects (so no duplicates).

how can comparing "id" of each object?

underscore.js has method called _.uniq(). looks correct, can't syntax "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" }]; 

you should able achieve _.uniq method used second parameter of property name used filter items:

var firstarray = [{ id: 1, name: "foo"}, { id: 2, name: "bar" }];  var secondarray = [{ id: 2, name: "boop" }, { id: 3, name: "baz" }];    var result = _.uniq(firstarray.concat(secondarray), 'id');    alert(json.stringify(result, null, 4));
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -