I have an object similar to the following, although nested a few letters deeper:
var foo = {
a:{
a:{},
b:{}
},
b:{
a:{},
b:{}
}
}
Given an array of arbitrary length containing 'a's and 'b's, for example ['b','a']
, I want to modify the object key mapped by the array letters. So with the array I just gave, I want to modify foo['b']['a'], or foo.b.a.
I can think of an easy way to get the target value, using something like the following:
var context = foo
for(letter in array){
context = context[letter]
}
return context
The issue with this is that if I tried to modify the final value of context, it won't modify the original object, which is what I want. Ideally if I had some method of creating pointers this would be simple, but I don't think javascript has them.
So is there any way to do this efficiently?