This question has already been answered:
- JSON path as string back to object 1 response
Object properties can be obtained as follows: obj['property'] . But what if the properties need to be obtained from a string (for example, 'pink.floyd.songs' ), we split('.') And get something like: obj['pink']['floyd']['songs'] . The problem is that we do not know prematurely how many investments there will be (the line could be, for example, like this: 'elbow.songs' ). How can you work with the properties of objects in this case? It is necessary not only to get the value of a certain Holy Island, but also to assign it some value. Those. we get some string as input, break it into an array of property names, we need to get something like this: obj['prop1']['prop2']['prop...'] = someValue . Based on the answer, I created a function for my task, who can come in handy:
function props(obj, arr) { var result = obj; for (var c = 0, l = arr.length; c < l - 1; c++) { result = result[arr[c]]; } return {obj: result, prop: arr[arr.length - 1]}; } var o = props(obj, props); o.obj[o.prop] = 'Some Value';
o.obj[o.prop] = 'Some Value';- I would send inside. And so, it seems the question already has an answer. What is the question? - Qwertiy ♦