首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度筛选列表对象到其他列表

角度筛选列表对象到其他列表
EN

Stack Overflow用户
提问于 2021-11-30 01:34:43
回答 1查看 232关注 0票数 0

我有一件定制的物品:

代码语言:javascript
复制
export class PartsChildInfo {
   name: string;
   materialName: string;
   thickNess: number;
}

export class PartGroupInfo
{
   materialName: string;
   thickNess: number;
}

例如,我有一个列表项PartsChildInfo

代码语言:javascript
复制
list : PartsChildInfo  = [
  { Name = "GA8-0608" , MaterialName = "SS"  , ThickNess = 1 };
  { Name = "05F1-051" , MaterialName = "SUS" , ThickNess = 2 };
  { Name = "2B73-002" , MaterialName = "AL"  , ThickNess = 3 };
  { Name = "01-20155" , MaterialName = "SS"  , ThickNess = 1 };
  { Name = "02MEG099" , MaterialName = "SUS" , ThickNess = 2 }; 
]

我希望在MaterialName中获得如下列表,ThickNess与list相同:

代码语言:javascript
复制
testChildList : PartGroupInfo = [
  { MaterialName = "SS"  , ThickNess = 1 };
  { MaterialName = "SUS" , ThickNess = 2 };
  { MaterialName = "AL"  , ThickNess = 3 }; 
]

我试过这个

代码语言:javascript
复制
testChildList : PartGroupInfo[] = [];
for (let i = 0; i < list.length; i++) {
   let targeti = list[i];
   for (let j = 0; j < this.testChildList.length; j++) {
      let targetj = this.testChildList[j];
      if (targeti.materialName != targetj.materialName && targeti.thickNess != targetj.thickNess) {
        let item = new PartGroupInfo();
        item.materialName = targeti.materialName;
        item.thickNess = targeti.thickNess;
        this.testChildList.push(item);
       }
    }
 }

但返回的列表为空。我该怎么修呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-30 01:57:27

也许使用.forEach来迭代list数组,通过index检查条目是否存在于testChildList中。当索引为-1 (不存在)时,将item推到testChildList

代码语言:javascript
复制
this.list.forEach((item) => {
  var i = this.testChildList.findIndex(
    (x) =>
      x.materialName == item.materialName && x.thickNess == item.thickNess
  );

  if (i == -1)
    this.testChildList.push({
      materialName: item.materialName,
      thickNess: item.thickNess,
    });
});

基于StackBlitz的样本解决方案

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70163265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档