0

I have a custom item:

export class PartsChildInfo {
   name: string;
   materialName: string;
   thickNess: number;
}

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

For example, I have a list item 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 }; 
]

I want to get the list as below with MaterialName, ThickNess the same from list :

testChildList : PartGroupInfo = [
  { MaterialName = "SS"  , ThickNess = 1 };
  { MaterialName = "SUS" , ThickNess = 2 };
  { MaterialName = "AL"  , ThickNess = 3 }; 
]

I have tried this

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);
       }
    }
 }

but the returned list is null. How should I fix it?

5
  • What is data[j]? Commented Nov 30, 2021 at 1:42
  • @YongShun i'm sr, i edited in post but still return null list Commented Nov 30, 2021 at 1:49
  • I guess this.testChildList.push(item); should be testChildList.push(item); Commented Nov 30, 2021 at 1:58
  • @N.F. thanks , but it still return null list Commented Nov 30, 2021 at 2:06
  • Which variable are you going to return? Commented Nov 30, 2021 at 2:07

1 Answer 1

1

Perhaps use .forEach to iterate the list array, check the item is existed in testChildList via index. Push the item to testChildList when index is -1 (no existed).

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,
    });
});

Sample Solution on StackBlitz

Sign up to request clarification or add additional context in comments.

5 Comments

at the first "testChildList" is empty list . and i want filter the "list " to get the element with both materialName and thickNess . can you watch it for me ?
Okay, let me summarize your requirement, you want to get materialName and thickNess from list and it is distinct?
yesss, it right
Hi, thanks for clarify the requirements and I had updated my answer.
I followed and it worked. thank you very much. I will put it as the answer !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.