My object is already defined. I want to insert variables at particular indexes in the object.
Given message object:
message=[{
person:{
actions:[{test:1},{test:2}]
},
person:{}
},
{
person:{
actions:[{test:3},{test:4}]
},
person:{}
}]
1) If my string = "message[0].person.actions[1].visible".
This means I need to insert visible=null
at index=1 of actions.
So message object becomes:
message=[{
person:{
actions:[{test:1},{test:2,visible:null}]
}
},
{
person:{
actions:[{test:3},{test:4}]
}
}]
2) If string="message[1].person.accessible",
So message Object become:
message=[{
person:{
actions:[{test:1},{test:2}]
}
},
{
person:{
accessible:null,
actions:[{test:3},{test:4}]
}
}]
I thought of doing split('[') and get that index and insert in that object. Let me know if there are any libraries out there simplifying this for more complex data structure.
This question got closed along with a link to question [Accessing nested JavaScript objects with string key My question is different than above mentioned link. I am asking about inserting a new field at particular location in object.