I have a nested object of this structure.
mainObject: {
1: {
id: 1,
name:'alpha'
},
2: {
id: 2,
name:'beta'
},
3: {
id: 3,
name:'gamma'
}
}
i want to delete on object from this ,say 1 but that in an immutable way . I tried the following code.
const list =Object.values(mainObject)
const newList=list.filter((item)=>{item.id!=1})
newList.forEach((item)=>{
mainObject[item.id]={
id: item.id,
name:item.name
}
})
this is not working .What am i doing wrong. Thanks in advance.
delete mainObject[1]doesn't work?const newList=list.filter((item)=>{item.id!=1})will ALWAYS be an empty list since your callback doesn't return anything