我在 Javascript 中有这个对象。
[{"col1": 1, "col2": 25},{"col1": 1, "col2": 25},{"col1": 3, "col2": 30}]
如何删除重复记录以获得此结果?
[{"col1": 1, "col2": 25},{"col1": 3, "col2": 30}]
我尝试了下一个逻辑,但不起作用:
[...new Set(myData)]
我最近在另一个答案上读到了这个,我喜欢它,它使用了filter()
作为this
过滤器函数内部传递的可选参数。
const input = [
{"col1": 1, "col2": 25},
{"col1": 1, "col2": 25},
{"col1": 3, "col2": 30}
];
let res = input.filter(function({col1, col2})
{
return !this.has(`${col1}-${col2}`) && this.add(`${col1}-${col2}`)
}, new Set());
console.log(res);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句