基于一个属性的唯一javascript数组

时间:2014-04-30 19:34:19

标签: javascript jquery multidimensional-array unique

我在javascript中有一个多维数组。我想让它显示每个唯一“名称”的第一条记录。例如:

[{"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%"},

会变成这样:

[{"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%"},

最初它不是多元的,我可以使用基本功能,但这种方法不适合这个。有任何想法吗?非常绝望!感谢。

2 个答案:

答案 0 :(得分:3)

这样的事情会创建一个只包含第一个唯一记录的新数组

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

答案 1 :(得分:0)

这应该可以解决问题:

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

如评论中所述,这要求对foo进行排序。如果foo未排序,则此方法有效:

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