问题描述
限时送ChatGPT账号..我正在尝试删除数组中的重复值对象但不起作用...我认为重复函数正在运行但未反映在 li
列表中.你能找出我必须改变的地方吗?
I am trying to remove duplicate value objects in an array but not working... I think duplicate function is working but not reflecting in li
list. Can you find out where I have to change?
我的服务文件:
addComp(Names,c){
this.item.push({ name: Names, componentid: c});
this.uniqueArray = this.removeDuplicates(this.item, "name"); //this line issue
this.item=this.uniqueArray; //this line issue
}
推荐答案
如果 addComp
是您修改 this.item
的唯一地方,只需在插入前检查是否存在.重复项永远不会放入数组中,因此您永远不必修剪它们.
If addComp
is the only place you modify this.item
, just check for existing prior to insertion. Duplicates will never get put in the array, so you'll never have to trim them.
addComp(Names,c){
let item = {name: Names, componentid: c};
if (this.item.find((test) => test.name === Names) === undefined) {
this.item.push(item);
}
}
或者,如果您正在修改this.item
的其他地方,您应该在更预期的地方去除重复项.作为 addComp
函数的副作用将它们剥离是出乎意料的.但是,你可以做到...
Alternatively, if there are other places that you're modifying this.item
, you should be stripping duplicates in a more expected place. Stripping them as a side-effect of the addComp
function is unexpected. However, you could do it...
addComp(Names,c){
this.item.push({name: Names, componentid: c});
this.item = this.item.filter((test, index, array) =>
index === array.findIndex((findTest) =>
findTest.name === test.name
)
);
}
这篇关于如何从角度为 6 的数组中删除重复的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论