Suppose I have this javascript object:
var obj = {
key_1: {
arr: [
999,
{
key_3: 5
}
]
},
key_2: 'value_2'
};
and I want to access the value 5.
What I would do is obj.key_1.arr[1].key_3.
But I want to have something like a json selector: ['key_1']['arr'][1]['key_3'] and dynamically apply it to the obj.
But I have no clue how to efficiently hold in a variable such a selector.
Also, I would like to have the ability to "concat" such selectors. For example:
// pseudocode
s1 = ['key_1']['arr'];
s2 = [1]['key_3'];
s = s1 + s2;
v = obj[s];
// v === 5
Is there a nice way to do something like this in javascript ?
['key_3']->[foo]. If you're not sure how deep the selector goes, then you'll need to key/index in step by step using a loop--you can't dynamically concat up a bunch of these and key into multiple levels deep in one step. Secondly, this seems like an XY problem. What is this supposed to accomplish? There might be a better way to achieve whatever you mean to achieve here.['key_1', 'arr', 1, 'key_3'and create a function that iterates the list and access one by one the properties of the object. I would like something more syntactically nice.lodash.getin the other thread, but this introduces a dependency and lodash uses a loop under the hood, so there's no way to do it without a loop.