Skip to content Skip to sidebar Skip to footer

Filter And Map The Property Of The Array Of Objects Based On Different Parameters In Angular 5

I would like to achieve the following by filtering and mapping the two array of nested objects using the map and filter in the angular. I have got one solution in regards to my las

Solution 1:

Here's how I would tackle it

const obj1 = [
   {
      "val":"type1",
      "removed":"N",
      "data":[
         {
            "label":"type1-a",
            "removed":"N",
            "dataid":16
         },
         {
            "label":"type1-b",
            "removed":"N",
            "dataid":26
         }
      ]
   },
   {
      "val":"type2",
      "removed":"N",
      "data":[
         {
            "label":"type2-a",
            "removed":"N",
            "dataid":12
         },
         {
            "label":"type2-b",
            "removed":"N",
            "dataid":34
         }
      ]
   },
   {
      "val":"type3",
      "removed":"N",
      "id":124,
      "label":"type3-label1"
   },
   {
      "val":"type4",
      "removed":"N",
      "id":126,
      "label":"type4-label1"
   },
   {
      "val":"type4",
      "removed":"N",
      "id":128,
      "label":"type4-label2"
   }
]

const obj2 = [
   {
      "val":"type1",
      "removed":"N",
      "data":[
         {
            "label":"type1-a",
            "removed":"N",
            "dataid":16
         },
         {
            "label":"type1-c",
            "removed":null,
            "dataid":null
         },
         {
            "label":"type1-d",
            "removed":null,
            "dataid":null
         }
      ]
   },
   {
      "val":"type3",
      "removed":"N",
      "id":124,
      "label":"type3-label1"
   },
   {
      "val":"type4",
      "removed":"N",
      "id":126,
      "label":"type4-label1"
   },
   {
      "val":"type3",
      "removed":null,
      "id":128,
      "label":"new"
   }
]


constgetIdentity = (obj) => {
  if(["type1", "type2"].includes(obj.val)) {
    return obj.val;
  }

  if(["type3", "type4"].includes(obj.val)) {
    return obj.id + obj.val;
  }
}

const result = obj1.reduce((acc, obj) => {

  const similarObj = obj2.find(nobj =>getIdentity(nobj) === getIdentity(obj));
  
  if(["type1", "type2"].includes(obj.val) && similarObj) {
    const data = obj.data.reduce((nacc, item) => {
      
      const similarItem = similarObj.data.find(pr => pr.dataid === item.dataid);
      
      if(!similarItem) {

        return [...nacc, {...item, removed: 'Y'}];
      }
      
      const newItem = {
        ...item,
        ...similarItem
      }
      
      return [...nacc, newItem];
    }, similarObj.data.filter(pr => !obj.data.some(npr => pr.dataid === npr.dataid)))
    
    const newObj = {
      ...obj,
      ...similarObj,
      data
    }
    
    return [...acc, newObj];
  }
  
  if(!similarObj) {
    acc = [...acc, {
      ...obj,
      removed: "Y"
    }];
    
    return acc;
  }
  
  return [...acc, obj];

}, obj2.filter(obj => !obj1.some(nobj =>getIdentity(obj) === getIdentity(nobj))))

result.sort((prev, next) => prev.val > next.val ? 1 : -1);

console.log(result);

Ideally every object would contain its unique id and type which decides if there should be additional processing. Thats my thought.

Post a Comment for "Filter And Map The Property Of The Array Of Objects Based On Different Parameters In Angular 5"