Pergunta

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.

Foi útil?

Solução

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

Outras dicas

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]);
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top