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?

1

4 Answers 4

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));

Sign up to request clarification or add additional context in comments.

4 Comments

What if ID was not contiguous?
@Adam Then there'll be a few empty list items. Those can easily be filtered out.
@Scimonster - it's a great solutions but, if you want to filtered out data on the bases of any string data than it'll not work.
@user3577774 in that case, you can use an object.
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;
    }
}()));

1 Comment

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;?
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));

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.