0

I am getting below response from API, but i want to format it for iteration to reach the correct values.

In below objects, i am getting column makeLineName and based on this column, i want to create one types array and push whole object as it is on types array.

Suppose for ex - In below response, We have 2 objects for "makeLineName":"Red", So i want to create one types array and push all object inside types array whose matching makeLineName = Red. Thats it.

const data = [{
"makeLineName":"Red",
"country":"Germany",
"processTypeId":"3",
"processTechType":"Batch & crunch"
},
{
"makeLineName":"Red",
"country":"Germany",
"processTypeId":"3",
"processTechType":"Batch"
},
{
"makeLineName":"Blue",
"country":"India",
"processTypeId":"3",
"processTechType":"Continues"
}
];

Expected Output

const data = [{
"makeLineName":"Red",
"processTypeId":"3",
"country":"Germany",
"processTechType":"Batch & crunch"
types :[{
"makeLineName":"Red",
"processTypeId":"3",
"country":"Germany",
"processTechType":"Batch & crunch"
},
{
"makeLineName":"Red",
"processTypeId":"3",
"country":"Germany",
"processTechType":"Batch"
}]
},
{
"makeLineName":"Blue",
"country":"India",
"processTypeId":"3",
"processTechType":"Continues"
types :[{
"makeLineName":"Blue",
"country":"India",
"processTypeId":"3",
"processTechType":"Continues"
}

}];

I did below code, it was working fine, but it is creating many nested arrays for types and because of this i am facing issue in iterating with loop to get the perfect value of types array.

getMakeLineMasterData() {
    const data = {
      data1: 'India'
    };  
    this.testfile.getWork(data).pipe(map(data => this.processData(data))).subscribe((resp: any) => {
      if (resp) {
        console.log(this.dataSource, "resp");
      }
    });
  }
  processData(data: any) {
    let mappedData = [];
    for (const item of data) {
      const mitem = mappedData.find(obj => obj.makeLineName == item.makeLineName);
      if (mitem) {
        mitem['types'].push(item);
      } else {
        let newItem = item;
        newItem['types'] = [item];
        mappedData.push(newItem);
      }
    }   
    return mappedData;
  }

Right now my code is working and returning data but it is creating many nested arrays for types inside and inside likewise.. Can anyone help me to make it proper as per my expected output.

1 Answers1

0

Issue: circular structure You are pushing the array inside itself.

eg:

         let object1 = { property: [] , cat:1 }; 
         object1.property = [object1];
or
       let object1 = { property: [object1]  }; 

        
        // here object1 is pointing to to itself, due to pass by reference.
       //that is why you are seeing a never ending nesting of itself (circular); 

Correction of your code:

let processData = (data: any)  => {
let mappedData = [];
for (const item of data) {
  const mitem = mappedData.find(obj => obj.makeLineName === item.makeLineName);
  if (mitem) {
    mitem['types'].push(item);
  } else {
    let newItem = { ... item }; //** creating a new object with copied properties
    newItem['types'] = [item];
    mappedData.push(newItem);
  }
}    
return mappedData; 
}
vaira
  • 2,191
  • 1
  • 10
  • 15
  • It was working correct but can this is possible to push all items on top object also which we are pushing inside types array? like my expected output.? Now it is pushing all elemnts under makeLine object and creating types array inside makeline object. – Tinku Tiwari Jan 08 '22 at 07:07
  • Can it is possible to create one object outside makeLine object for all keys and then create rest. can u plz see my expected output. – Tinku Tiwari Jan 08 '22 at 07:10
  • Updated, but the point was saying `it was working correct` is a bad way to look at code, either its working or its working wrong. If you have understood the answer you would not have asked me to do a such small change for you. – vaira Jan 08 '22 at 07:41