如何从角度为 6 的数组中删除重复的对象

编程入门 行业动态 更新时间:2024-10-21 19:03:18
本文介绍了如何从角度为 6 的数组中删除重复的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-22 17:09:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1027010.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组中   角度   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!