I am looking for the best solution to update the key values inside the nested objects in angular.
The solution which I know is manually running 2 for loops and updating each property.
below is what I have tried and what I want to achieve.
Are there any options to avoid ending up running 2 loops?
I was thinking to use map option inside the loop but not getting what I expected.
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
}
]
}
]
Result =
obj1 = [
{
"val":"type1",
"removed":"N",
"data":[
{
"newlabel":"type1-a",
"removed":"N",
"newid":16,
"extraparam1":null
},
{
"newlabel":"type1-b",
"removed":"N",
"newid":26,
"extraparam1":null
}
]
},
{
"val":"type2",
"removed":"N",
"data":[
{
"newlabel":"type2-a",
"removed":"N",
"newid":12,
"extraparam1":null
},
{
"newlabel":"type2-b",
"removed":"N",
"newid":34,
"extraparam1":null
}
]
}
]
obj1.forEach(val=>{
if(val.data){
//logic to modify the existing object
}
});