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"
}
]
removedfield changed to 'Y'. Please explain what am I missing if this not the result you want.