0

I have a multidimensional array in javascript. I want to make it so it shows the first record for each unique "name". So for example this:

[{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},
{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},
{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},
{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},
{"name":"Aellen , Nick","col1":"Director","col2":"test","col3":"test","col4":"96.88%"},
{"name":"Armstrong , Peter","col1":"Director","col2":"test","col3":"test","col4":"95.15%"},
{"name":"Ashmore , Abigail","col1":"Vice President","col2":"test","col3":"test","col4":"99.80%"},
{"name":"Avent , Christopher","col1":"Vice President","col2":"test","col3":"test","col4":"90.11%"},
{"name":"Baker , Ian Howard","col1":"Director","col2":"test","col3":"test","col4":"67.20%"},
{"name":"Baker , Ian Howard","col1":"Director","col2":"test","col3":"test","col4":"67.20%"},

Would turn into this:

[{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},
{"name":"Aellen , Nick","col1":"Director","col2":"test","col3":"test","col4":"96.88%"},
{"name":"Armstrong , Peter","col1":"Director","col2":"test","col3":"test","col4":"95.15%"},
{"name":"Ashmore , Abigail","col1":"Vice President","col2":"test","col3":"test","col4":"99.80%"},
{"name":"Avent , Christopher","col1":"Vice President","col2":"test","col3":"test","col4":"90.11%"},
{"name":"Baker , Ian Howard","col1":"Director","col2":"test","col3":"test","col4":"67.20%"},

Originally It was not multivarite and i could use a basic function, but that method wont work for this one. Any ideas? Pretty desperate! Thanks.

2
  • It's actually an array of objects as opposed to a multi-dimensional array... Commented Apr 30, 2014 at 19:40
  • Sorry .. My bad, been going all day :p Commented Apr 30, 2014 at 20:12

2 Answers 2

3

Something like this would create a new array with the first unique records only

var obj     = {},
    new_arr = [];

arr.forEach(function(itm) {
    if ( ! (itm.name in obj) ) obj[itm.name] = itm;
});

for (var key in obj) {
    new_arr.push(obj[key]);
}

FIDDLE

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

2 Comments

Why don't you just do new_arr.push(obj)? Instead of for in loop.
@RahilWazir - because I'm not pushing that object, but the objects contained in that object etc. Just using the names as keys to make sure they are unique.
0

This should do the trick:

var foos = [{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},
{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},
{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},
{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},
{"name":"Aellen , Nick","col1":"Director","col2":"test","col3":"test","col4":"96.88%"},
{"name":"Armstrong , Peter","col1":"Director","col2":"test","col3":"test","col4":"95.15%"},
{"name":"Ashmore , Abigail","col1":"Vice President","col2":"test","col3":"test","col4":"99.80%"},
{"name":"Avent , Christopher","col1":"Vice President","col2":"test","col3":"test","col4":"90.11%"},
{"name":"Baker , Ian Howard","col1":"Director","col2":"test","col3":"test","col4":"67.20%"},
{"name":"Baker , Ian Howard","col1":"Director","col2":"test","col3":"test","col4":"67.20%"}];

var new_foos = [];

var previous_foo = null;

for (var i = 0; i < foos.length; i++) {
    if (previous_foo != foos[i].name)
    {
        new_foos.push(foos[i]);
    }

    previous_foo = foos[i].name;
}

As mentioned in the comments, this requires that foo is sorted. This works if foo is unsorted:

var foos = [{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},
{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},

{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},
{"name":"Baker , Ian Howard","col1":"Director","col2":"test","col3":"test","col4":"67.20%"},
{"name":"Ashmore , Abigail","col1":"Vice President","col2":"test","col3":"test","col4":"99.80%"},
{"name":"Baker , Ian Howard","col1":"Director","col2":"test","col3":"test","col4":"67.20%"},
{"name":"Aellen , Nick","col1":"Director","col2":"test","col3":"test","col4":"96.88%"},
{"name":"Armstrong , Peter","col1":"Director","col2":"test","col3":"test","col4":"95.15%"},{"name":"Adelman , Erica","col1":"Vice President","col2":"test","col3":"test","col4":"73.77%"},
{"name":"Avent , Christopher","col1":"Vice President","col2":"test","col3":"test","col4":"90.11%"}];

var new_foos = [];

for (var i = 0; i < foos.length; i++) {
    var new_foo = true;
    for (var j = 0; j < new_foos.length; j++) {
        if (foos[i].name == new_foos[j].name) {
            new_foo = false;
            break;
        }
    }

    if (new_foo)
    {
        new_foos.push(foos[i]);
    }
}

10 Comments

Why the solution is in Python? :D
Apparently it has been a loooong day D:
@War10ck True, I suppose, but it's still usable pseudocode I'd say. I would hope for people to understand the code they're copying and not just copy-paste, so it shouldn't be a big effort to rewrite in JS if you have some level of understanding.
@Katana314 While this may be true, I don't think it's entirely clear if you're not familiar with python. I've written both so I understand what you mean. For someone who hasn't though, I think it could be a little difficult to follow. It's for the reason you said though, that I did not downvote the post and merely commented on it.
I would note that this algorithm only works if the array is sorted by name.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.