繁体   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