Angular - 将列表对象过滤到其他列表
Angular - Filter list object to other list
我有一个自定义项:
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
与列表相同:
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);
}
}
}
但返回的列表为空。我该如何解决?
也许使用.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,
});
});
我有一个自定义项:
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
与列表相同:
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);
}
}
}
但返回的列表为空。我该如何解决?
也许使用.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,
});
});