猿问

从JSON数组中删除重复的对象

我有一个看起来像这样的数组:


var standardsList = [

    {"Grade": "Math K", "Domain": "Counting & Cardinality"},

    {"Grade": "Math K", "Domain": "Counting & Cardinality"},

    {"Grade": "Math K", "Domain": "Counting & Cardinality"},

    {"Grade": "Math K", "Domain": "Counting & Cardinality"},

    {"Grade": "Math K", "Domain": "Geometry"},

    {"Grade": "Math 1", "Domain": "Counting & Cardinality"},

    {"Grade": "Math 1", "Domain": "Counting & Cardinality"},

    {"Grade": "Math 1", "Domain": "Orders of Operation"},

    {"Grade": "Math 2", "Domain": "Geometry"},

    {"Grade": "Math 2", "Domain": "Geometry"}

];

而且我需要删除重复项,以便保留类似以下内容:


var standardsList = [

    {"Grade": "Math K", "Domain": "Counting & Cardinality"},

    {"Grade": "Math K", "Domain": "Geometry"},

    {"Grade": "Math 1", "Domain": "Counting & Cardinality"},

    {"Grade": "Math 1", "Domain": "Orders of Operation"},

    {"Grade": "Math 2", "Domain": "Geometry"}

];

我曾尝试安装underscore.js并使用._uniq,但这似乎仅key:value在对象中出现一对时才起作用。我似乎无法让它跨多个键工作。


当我尝试类似的东西:


var uniqueStandards = _.uniq(standardsList, function(item, key, Domain){

    return item.Domain;

});

我只得到前三个唯一值(每个年级一个)。但是我需要所有年级和领域的唯一值。是否有一种简单的方法将两个键都馈给_.uniq函数?


最终,我需要一个列表,其中每个唯一的等级作为标题,唯一的域作为列表项,以传递到HTML页面中。我可能会犯这个错误,因此,如果有一种更简单的方法来实现最终目标,则可以公开征求意见。


提前致谢!


编辑:得到一些好的回应,并想澄清我的最终目标是什么。我正在尝试以HTML形式创建一系列列表:


<div>

    <h3>Math K</h3>

    <li>Counting & Cardinality</li>

    <li>Geometry</li>

</div>

<div>

    <h3>Math 1</h3>

    <li>Counting & Cardinality</li>

    <li>Orders of Operation</li>

</div>

<div>

    <h3>Math 2</h3>

    <li>Geometry</li>

</div>

我最初的想法是创建一个数组并将其推入<div>页面中的元素中,$("#divid").append(array)


慕桂英4014372
浏览 797回答 3
3回答

隔江千里

function arrUnique(arr) {&nbsp; &nbsp; var cleaned = [];&nbsp; &nbsp; arr.forEach(function(itm) {&nbsp; &nbsp; &nbsp; &nbsp; var unique = true;&nbsp; &nbsp; &nbsp; &nbsp; cleaned.forEach(function(itm2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (_.isEqual(itm, itm2)) unique = false;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; if (unique)&nbsp; cleaned.push(itm);&nbsp; &nbsp; });&nbsp; &nbsp; return cleaned;}var standardsList = arrUnique(standardsList);这将返回var standardsList = [&nbsp; &nbsp; {"Grade": "Math K", "Domain": "Counting & Cardinality"},&nbsp; &nbsp; {"Grade": "Math K", "Domain": "Geometry"},&nbsp; &nbsp; {"Grade": "Math 1", "Domain": "Counting & Cardinality"},&nbsp; &nbsp; {"Grade": "Math 1", "Domain": "Orders of Operation"},&nbsp; &nbsp; {"Grade": "Math 2", "Domain": "Geometry"}];您到底要哪个?

偶然的你

恢复了一个老问题,但是我想在@adeneo的答案上发布一个迭代。这个答案是完全笼统的,但是对于这种用例,它可能会更有效(在我的机器上有数千个对象的数组中,速度很慢)。如果您知道需要比较的对象的特定属性,则直接比较它们:var sl = standardsList;var out = [];for (var i = 0, l = sl.length; i < l; i++) {&nbsp; &nbsp; var unique = true;&nbsp; &nbsp; for (var j = 0, k = out.length; j < k; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if ((sl[i].Grade === out[j].Grade) && (sl[i].Domain === out[j].Domain)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unique = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (unique) {&nbsp; &nbsp; &nbsp; &nbsp; out.push(sl[i]);&nbsp; &nbsp; }}console.log(sl.length); // 10console.log(out.length); // 5
随时随地看视频慕课网APP
我要回答