matt matt - 2 years ago 62
Javascript Question

Checking for duplicate entries in array of objects and creating a new one

I'm trying to merge duplicate entries in an array of objects by creating a new array.

I don't know how to check for duplicated without looping through the new array, which then messes me up when trying to push stuff because it does it every time for the outer loop.

How should I do this properly?

var arr=[0];
for (var k in data) {
var name = data[k].name;
var item = data[k];
//check if already in array
for (var l in arr){
if (arr[l].name == name){
arr[l].value = arr[l.value] + ';;' + item.value;
} else {

Answer Source

With Lodash

var arr = _.uniqBy(data, 'name');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download