Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
How to search array of objects and push new value in array Angular 8

I have two different response from API. Below response contain lineId and Name.

this.lines = [
    {      
        lineId: "R_X002_ACCESS"
        localName: "ACCESS"
        name: "ACCESS"
    },
    {      
        lineId: "R_X00R_X002_BIB2_ACCESS"
        localName: "BIB"
        name: "BIB"
    },
    {      
        lineId: "R_X002_KNORR"
        localName: "Knorr"
        name: "Knorr"
    },
     {      
        lineId: "R_X002_POWDER"
        localName: "Powder"
        name: "Powder"
    },
];

This response is for processData function, Here i wanted to search name from this.lines api response based on lineId of item object and if matches then need to push Name

item = {
lineId: "R_X002_POWDER"
},
{
lineId: "R_X00R_X002_BIB2_ACCESS,R_X002_ACCESS"
},
{
lineId: "R_X002_POWDER"
};

Now in below code , i am searching name based on lineId from this.lines api response and if it matches then trying to push inside plist array.

Below is my code, here i am passing api response and preparing array based on some condition.

I tried below code inside processData function, but it is not working for comma seprated valuesand also not pushing to proper plist array.

var lineName = this.lines.filter(function(line) {
        if(line.lineId === item.lineId){
         return line.name;
        }
      });




processData(data: any) {
    let mappedData = [];
    for(const item of data){
      console.log(item,"item");
      var lineName = this.lines.filter(function(line) {
        if(line.lineId === item.lineId){
         return line.name;
        }
      });
      const mitem = mappedData.find(obj => obj.makeLineName == item.makeLineName);
      if(mitem){
        mitem['plist'].push(item);
      } else {
        let newItem = item;
        newItem['plist'] = [ item ];
        mappedData.push(newItem);
      }
    }
    return mappedData;
  }

Expected output

 lineId: "R_X002_POWDER",
     name: "Powder"
    },
    {
    lineId: "R_X00R_X002_BIB2_ACCESS,R_X002_ACCESS",
     name: "BIB","ACCESS"
    },
    {
    lineId: "R_X002_KNORR",
     name: "Knorr"
    };
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I think because of lineId returned to you, instead of checking lineId equality you should check if the incoming lineId includes your lineId.
in your code: instead of line.lineId === item.lineId
check this: (item.lineId).includes(line.lineId)

maybe it works...

about 4 years ago · Juan Pablo Isaza Denunciar

0

Does this work for you(mapNames function)...

const lines = [
    {
        lineId: "R_X002_ACCESS",
        localName: "ACCESS",
        name: "ACCESS"
    },
    {
        lineId: "R_X00R_X002_BIB2_ACCESS",
        localName: "BIB",
        name: "BIB"
    },
    {
        lineId: "R_X002_KNORR",
        localName: "Knorr",
        name: "Knorr"
    },
    {
        lineId: "R_X002_POWDER",
        localName: "Powder",
        name: "Powder"
    },
];

const items = [
    {
        lineId: "R_X002_POWDER"
    },
    {
        lineId: "R_X00R_X002_BIB2_ACCESS,R_X002_ACCESS"
    },
    {
        lineId: "R_X002_POWDER"
    }
];

function mapNames(lines, items) {
    const mappedLines = {};
    
    lines.forEach(lineItem => {
        if (!mappedLines[lineItem.lineId]) {
            mappedLines[lineItem.lineId] = lineItem;
        }
    });
    
    const mappedItems = items
        .map(item => {
        return {
            lineId: item.lineId,
            name: item.lineId.split(",")
                .map(lineItem => mappedLines[lineItem].name || "")
                .filter(x => x)
                .join(",")
        };
    });
    return mappedItems;
}

console.log("Mapped Names:\n", mapNames(lines, items));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda