1

Hi I am trying to compare two array of objects and want to achieve the custom array of object by manipulating it.

I would like to achieve something like this by checking each time anything from object1 is removed or not? if it is removed then it should change attribute to 'Y'.

object 1 = [
   {
      "label":"a",
      "removed":"N",
      "value":1
   },
   {
      "label":"b",
      "removed":"N",
      "value":2
   }
]

object 2 =[
   {
      "label":"a",
      "removed":"N",
      "value":1
   },
   {
      "label":"c",
      "removed":"N",
      "value":3
   }
]

result should be =

[{
label:"a",
removed:"N",
value:1
},{
label:"b",
removed:"Y",
value:2
},{
label:"c",
removed:"N",
value:3
}]

I have tried to loop both array and tried to achieve the same but it is somehow not working.

I tried following code.

let data = []

object1.forEach((item1) => {

            object2.forEach((item2) => {

                    if (item1.value === item2.value) {
                        data.push(Object.assign(item1));
                    } else {
                        item2.removeFlag = 'Y';
                        data.push(Object.assign(item1, item2));

                    }
                }

            }

...Updated Question.....

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

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":"type4",
      "removed":null,
      "id":null,
      "label":"type4-label3"
   }
]

result = [
   {
      "val":"type1",
      "removed":"N",
      "data":[
         {
            "label":"type1-a",
            "removed":"N",
            "dataid":16
         },
         {
            "label":"type1-b",
            "removed":"Y",
            "dataid":26
         },
         {
            "label":"type1-c",
            "removed":null,
            "dataid":null
         },
         {
            "label":"type1-d",
            "removed":null,
            "dataid":null
         }
      ]
   },
   {
      "val":"type2",
      "removed":"Y",
      "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":"Y",
      "id":128,
      "label":"type4-label2"
   },
   {
      "val":"type4",
      "removed":null,
      "id":null,
      "label":"type4-label3"
   }
]

4
  • 1
    You want the result array to contain the values that have the same 'label' in both arrays, but also the object in array 1 that is not is array 2 with the removed field changed to 'Y'. Please explain what am I missing if this not the result you want. Commented May 21, 2020 at 20:44
  • yes exactly, I am looking for the result array which will track object1 and if the object1 is missing in object2 it will add and change the change removed field to 'Y'. It is basically tracking if something is removed from the object1. Commented May 21, 2020 at 20:47
  • You want to check the equality by the field 'label' right? Commented May 21, 2020 at 20:49
  • by value or label. Commented May 21, 2020 at 20:50

1 Answer 1

1

const object1 = [{
    "label": "a",
    "removed": "N",
    "value": 1
  },
  {
    "label": "b",
    "removed": "N",
    "value": 2
  }
]

const object2 = [{
    "label": "a",
    "removed": "N",
    "value": 1
  },
  {
    "label": "c",
    "removed": "N",
    "value": 3
  }
]

const result = [...object2.map(record => {
  const record2 = object1.find(pr => pr.label === record.label) || {};

  return {
    ...record,
    ...record2
  }
}), ...object1.filter(pr => !object2.some(npr => npr.label === pr.label)).map(pr => ({ ...pr,
  removed: "Y"
}))]

console.log(result);

--Edit

With nested data you have to repeat the same code inside reduce function.

Example


const result = [...object2.map(record => {
  const record2 = object1.find(pr => pr.val === record.val) || {};

  const data = [...(record.data || []).map(pr => ({ ...pr,
      ...(record2.data.find(npr => npr.label === pr.label) || {})
    })),
    ...(record2.data || []).filter(pr => !record.data.some(npr => npr.label === pr.label)).map(pr => ({ ...pr,
      removed: 'Y'
    }))
  ]

  return {
    ...record,
    ...record2,
    data

  }
}), ...object1.filter(pr => !object2.some(npr => npr.val === pr.val)).map(pr => ({ ...pr,
  removed: "Y"
}))]
Sign up to request clarification or add additional context in comments.

12 Comments

thanks Jozef, I have one question in regards to nested array of objects, can we use same compare method or we need to iterate differently.
Updated answer. There will be just more logic in reduce function
I think it would be much better to start a new question
please check above link. @Józef Podlecki
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.