簡體   English   中英

如何從javascript中的列表中刪除重復項

[英]How to remove duplicates items from the list in javascript

名單是,

{id: 11, type: "sell", quantity: 11, price: 155}
{id: 11, type: "sell", quantity: 11, price: 155}
{id: 11, type: "sell", quantity: 11, price: 155}
{id: 12, type: "buy", quantity: 3, price: 189}
{id: 13, type: "buy", quantity: 4, price: 189}
{id: 14, type: "buy", quantity: 2, price: 189}
{id: 14, type: "buy", quantity: 2, price: 189}

(近 1000 項)我想從 javascript 中的列表中刪除重復項,例如,因為 id 11 和 14 的項有重復項,所以新列表將在刪除后,

  {id: 12, type: "buy", quantity: 3, price: 189}
    {id: 13, type: "buy", quantity: 4, price: 189}

重復項將在新數組中完全刪除,而不像它仍然存在於新數組中

您可以使用Set作為已經訪問過的id閉包,並從結果集中刪除該對象(如果存在)。

此方法對數據使用單個循環,並為每個發現的重復項進行過濾。

 var array = [{ id: 11, type: "sell", quantity: 11, price: 155 }, { id: 11, type: "sell", quantity: 11, price: 155 }, { id: 11, type: "sell", quantity: 11, price: 155 }, { id: 12, type: "buy", quantity: 3, price: 189 }, { id: 13, type: "buy", quantity: 4, price: 189 }, { id: 14, type: "buy", quantity: 2, price: 189 }, { id: 14, type: "buy", quantity: 2, price: 189 }], single = array.reduce((s => (r, o) => { if (s.has(o.id)) { return r.filter(({ id }) => id !== o.id); } s.add(o.id); r.push(o); return r; })(new Set), []); console.log(single);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

一個簡單的解決方案是在將項目轉換為字符串后散列這些項目,並查看它們是否已經存在於目標數組中,如果沒有,則添加它們。

 String.prototype.hashCode = function() { var hash = 0, i, chr; if (this.length === 0) return hash; for (i = 0; i < this.length; i++) { chr = this.charCodeAt(i); hash = ((hash << 5) - hash) + chr; hash |= 0; // Convert to 32bit integer } return hash; }; Array.prototype.isUnique = function(obj){ if (this.length <= 0) return true; let hash = JSON.stringify(obj).hashCode(); return !this.some(function(e){ return JSON.stringify(e).hashCode() === hash; }); } let test = [{id: 11, type: "sell", quantity: 11, price: 155}, {id: 11, type: "sell", quantity: 11, price: 155}, {id: 11, type: "sell", quantity: 11, price: 155}, {id: 12, type: "buy", quantity: 3, price: 189}, {id: 13, type: "buy", quantity: 4, price: 189}, {id: 14, type: "buy", quantity: 2, price: 189}, {id: 14, type: "buy", quantity: 2, price: 189}]; let out = []; test.forEach(function(item){ if (out.isUnique(item)) out.push(item); }); console.log(out);

這個問題已被否決,但我認為我從未真正看到過關於如何使用對象鍵執行此操作的簡潔答案。 如果您不需要 IE11,這個答案應該有效。 它使用 ES6 findIndex函數。

 function removeDuplicatesFromArray(arr){ let unique_array = arr.filter(function(elem, index, self) { let firstOccurence = self.findIndex(elem => elem.id == self[index].id) return firstOccurence == index; }); return unique_array } var duplicatesArr = [{id: 11, type: "sell", quantity: 11, price: 155}, {id: 11, type: "sell", quantity: 11, price: 155}, {id: 11, type: "sell", quantity: 11, price: 155}, {id: 12, type: "buy", quantity: 3, price: 189}, {id: 13, type: "buy", quantity: 4, price: 189}, {id: 14, type: "buy", quantity: 2, price: 189}, {id: 14, type: "buy", quantity: 2, price: 189}] console.log(removeDuplicatesFromArray(duplicatesArr));

還:

我建議檢查一些其他可用的內置數組函數。 其中大部分是 ES2015,因此與 IE11 兼容。 我的具體答案使用了Array.prototype.filter ,但您也可以使用map等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM