亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從JSON數組中刪除重復的對象

從JSON數組中刪除重復的對象

慕桂英4014372 2019-10-09 17:35:41
我有一個看起來像這樣的數組: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)
查看完整描述

3 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

function arrUnique(arr) {

    var cleaned = [];

    arr.forEach(function(itm) {

        var unique = true;

        cleaned.forEach(function(itm2) {

            if (_.isEqual(itm, itm2)) unique = false;

        });

        if (unique)  cleaned.push(itm);

    });

    return cleaned;

}


var standardsList = arrUnique(standardsList);

這將返回


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

];

您到底要哪個?


查看完整回答
反對 回復 2019-10-09
?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

恢復了一個老問題,但是我想在@adeneo的答案上發布一個迭代。這個答案是完全籠統的,但是對于這種用例,它可能會更有效(在我的機器上有數千個對象的數組中,速度很慢)。如果您知道需要比較的對象的特定屬性,則直接比較它們:


var sl = standardsList;

var out = [];


for (var i = 0, l = sl.length; i < l; i++) {

    var unique = true;

    for (var j = 0, k = out.length; j < k; j++) {

        if ((sl[i].Grade === out[j].Grade) && (sl[i].Domain === out[j].Domain)) {

            unique = false;

        }

    }

    if (unique) {

        out.push(sl[i]);

    }

}


console.log(sl.length); // 10

console.log(out.length); // 5


查看完整回答
反對 回復 2019-10-09
  • 3 回答
  • 0 關注
  • 822 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號