3

I have the following JS array:

var items=[
    { id: 1, name: 'aa' },
    { id: 1, name: 'bb' },
    { id: 2, name: 'cc' },
    { id: 1, name: 'dd' }
];

I wan to convert it to the following.

var items=[
    { id: 1, name: 'dd' },
    { id: 2, name: 'cc' }
];

How should I go about it using JavaScript?

Shreerang
  • 367
  • 4
  • 15

4 Answers4

1

We could actually do this quite simply, just looping over it. For each item, assign its place in the new array corresponding to its ID. Earlier IDs get overwritten, so only the last one is saved.

var items=[
    { id: 1, name: 'aa' },
    { id: 1, name: 'bb' },
    { id: 2, name: 'cc' },
    { id: 1, name: 'dd' }
], newItems = [];
for (var i =0;i<items.length;i++){
    newItems[items[i].id - 1] = items[i];
}
alert(JSON.stringify(newItems));
Scimonster
  • 32,893
  • 9
  • 77
  • 89
0

Try this:

items = items.reverse().filter((function() {
    var existing = {};
    return function(item) {
        if(existing[item.id]) {
            return false;
        }
        existing[item.id] = true;
        return item;
    }
}()));
friedi
  • 4,350
  • 1
  • 13
  • 19
  • Since we are filtering, yes, let's use `filter`!! The inside could be cleaned up a little bit, however. How about `return !existing[item.id] && existing[item.id] = true;`? –  Sep 30 '14 at 11:27
0

This should do what you want:

var items = [
    { id: 1, name: 'aa' },
    { id: 1, name: 'bb' },
    { id: 2, name: 'cc' },
    { id: 1, name: 'dd' }
];

function removeDupIds(items){
    var result  = [],
        uniqIds = {},
        i       = 0,
        l       = items.length;

    for(; i<l; i++){
        uniqIds[items[i].id] = items[i].name;
    }

    for(var k in uniqIds){
        result.push({id: k, name: uniqIds[k]});
    }
    return result;
}

console.log(removeDupIds(items));
Larry Lee
  • 126
  • 6
0

Using underscore, it would just be

_.uniq(items.reverse(), function(i1, i2) { return i1.id === i2.id; }))

Normally _.uniq removes identical items, but we can pass it the second parameter specifying a function to use to determine if two items are going to be considered identical.

The items.reverse() is needed because the OP seems to want the final item to take priority.