Home > Enterprise >  How to search array of objects and push new value in array Angular 8
How to search array of objects and push new value in array Angular 8

Time:01-24

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

CodePudding user response:

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...

CodePudding user response:

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

  •  Tags:  
  • Related