Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找角度为8的数组中的所有重复项_Javascript_Typescript - Fatal编程技术网

Javascript 查找角度为8的数组中的所有重复项

Javascript 查找角度为8的数组中的所有重复项,javascript,typescript,Javascript,Typescript,我正在安圭拉8号的附件表单列表中查找所有副本。我搜索了一些帖子,下面是我可以找到的代码,但我仍然无法将名称:Document1的isDuplicate设置为true。我非常感谢你在这方面的帮助 export interface AttachmentForm { name: string; isDuplicate: boolean; } ngOnInit() { this.attachmentForms = [ {name: 'Document1', isDupl

我正在安圭拉8号的
附件表单
列表中查找所有副本。我搜索了一些帖子,下面是我可以找到的代码,但我仍然无法将名称:
Document1
isDuplicate
设置为
true
。我非常感谢你在这方面的帮助

export interface AttachmentForm {
  name: string;
  isDuplicate: boolean;
}

  ngOnInit() {
    this.attachmentForms = [
      {name: 'Document1', isDuplicate: false},
      {name: 'Document2', isDuplicate: false},
      {name: 'Document1', isDuplicate: false},
      {name: 'Document3', isDuplicate: false},
    ];
   this.findDuplicates() ;
  }
 findDuplicates() {
    const newArr: AttachmentForm[] = [];
    this.attachmentForms.forEach((item, index) => {
      if (newArr.findIndex(i => i.name === item.name) === -1) {
        item.isDuplicate = false;
      } else {
        item.isDuplicate = true;
      }
      newArr.push(item);
    });
    this.attachmentForms = newArr;
  }



尝试将
findDuplicates()
更改为
this.findDuplicates()


下面是一个Stackblitz示例:

是否
findDuplicates
被调用?您也不需要执行
newArr.push(项目)
if
else
语句中,您可以在这两条语句之后执行,因为条件语句是同步的。很像
for
循环之后的任何内容。@MattCroak我已根据您的评论更新了问题,感谢您让我了解有关更改。在调用
findDuplicates
时添加此内容是我在发布此问题时犯的编辑错误。谢谢您让我知道。t我仍然无法将名称:
Document1
isDuplicate
设置为true。您能否帮助仅一个
Document1
行具有
isDuplicate=true
,其他
Document1
行具有
isDuplicate=false
@3limin4t0r和@shareedit给出了一个更好的解决方案,其中
Document1
行具有
isDuplicate=true
,不幸的是,他们删除了答案:下面是代码:
const counter=new Map();forEach(({name})=>counter.set(name,(counter.get(name)| | 0)+1));this.attachmentForms.forEach(item=>(item.isDuplicate=counter.get(item.name)!==1))我已经更新了stackblitz和示例代码,以生成您想要的结果。我尝试了你发布的代码,但没有得到你在我的堆栈闪电战中想要的结果。我可能漏了一行
ngOnInit() {
    this.attachmentForms = [
        { name: 'Document1', isDuplicate: false },
        { name: 'Document2', isDuplicate: false },
        { name: 'Document1', isDuplicate: false },
        { name: 'Document3', isDuplicate: false },
    ];
    this.findDuplicates();
}

findDuplicates() {
    const newArr: AttachmentForm[] = [];
    this.attachmentForms.forEach((item) => {
        if (this.attachmentForms.filter(i => i.name === item.name).length > 1) {
            item.isDuplicate = true;
        } else {
            item.isDuplicate = false;
        }
        newArr.push(item);
    });
    this.attachmentForms = newArr;
}