es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Angular: filtrar objeto de lista a otra lista.

Tengo un artículo personalizado:

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

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

Por ejemplo, tengo un elemento de lista 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 }; 
]

Quiero obtener la lista de abajo con MaterialName y ThickNess iguales a los de la lista:

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

He intentado esto

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

pero la lista devuelta es nula. ¿Cómo puedo solucionarlo?

Tags:  , , , ,

Answer

  1. Avatar for davy.ai

    El problema con el código proporcionado es que testChildList es inicialmente un arreglo vacío, por lo que el bucle interno nunca se ejecutará para verificar si targeti coincide con algún valor en testChildList. Una posible solución para lograr el resultado deseado es crear primero un testChildList vacío e iterar a través de list para verificar si el PartsChildInfo actual coincide con algún PartGroupInfo en testChildList. Si no es así, se crea un nuevo objeto PartGroupInfo y se agrega a testChildList. El código corregido podría verse así:

    testChildList: PartGroupInfo[] = [];
    
    for (let i = 0; i < list.length; i++) {
       let targeti = list[i];
       let found = false;
       for (let j = 0; j < this.testChildList.length; j++) {
          let targetj = this.testChildList[j];
          if (targeti.materialName === targetj.materialName && targeti.thickNess === targetj.thickNess) {
             found = true;
             break;
          }
       }
       if (!found) {
          let item = new PartGroupInfo();
          item.materialName = targeti.materialName;
          item.thickNess = targeti.thickNess;
          this.testChildList.push(item);
       }
    }
    

    Aquí, la bandera found se usa para realizar un seguimiento si el objeto PartsChildInfo actual coincide con algún objeto PartGroupInfo en testChildList. Si el bucle for completa sin encontrar una coincidencia, se crea un nuevo objeto PartGroupInfo y se agrega a testChildList.

Comments are closed.