我有一件定制的物品:
export class PartsChildInfo {
name: string;
materialName: string;
thickNess: number;
}
export class PartGroupInfo
{
materialName: string;
thickNess: number;
}例如,我有一个列表项PartsChildInfo
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相同:
testChildList : PartGroupInfo = [
{ MaterialName = "SS" , ThickNess = 1 };
{ MaterialName = "SUS" , ThickNess = 2 };
{ MaterialName = "AL" , ThickNess = 3 };
]我试过这个
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);
}
}
}但返回的列表为空。我该怎么修呢?
发布于 2021-11-30 01:57:27
也许使用.forEach来迭代list数组,通过index检查条目是否存在于testChildList中。当索引为-1 (不存在)时,将item推到testChildList。
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,
});
});https://stackoverflow.com/questions/70163265
复制相似问题