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



我有一个多维数组在javascript。我想让它显示每个唯一的"name"的第一个记录。例如:

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

最初它不是多变量的,我可以使用一个基本的函数,但这种方法对这个不起作用。什么好主意吗?很绝望!谢谢。

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

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

小提琴

这应该能奏效:

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进行排序。

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

相关内容

  • 没有找到相关文章

最新更新