0
votes

I would like to access the object provided only it's string path in form of array is known.

1.) there is an object, where

root["obj1"]["obj2"] = 1;

(in common case root["obj1"]...["objN"])

2.) I have ONLY string objectPath known:

var objectPath = 'root["obj1"]["obj2"]'

3.) I need NOT only READ the object, but SET it's value, like

objectPath = 2;
//so root["obj1"]["obj2"] === 2

As I understand

  1. there might be some options with eval(), but it gets the value, not the variable;

  2. one can loop through all objects of root, make convertion to "root.obj1.obj2" (which is not the case, as "obj1" can easily be "obj with spaces1") and check if given string equals to current object in the loop.

http://jsfiddle.net/ACsPn/

Related Link: Access object child properties using a dot notation string

1
yes, with eval(), i need to reset the object, change the 1 to 2 for example. - Max
The accepted answer in the question you link to is much better than eval. It contains almost everything you need, you just have to use a different splitting and change it to a set(path,value) function. - Denys Séguret
one char "." against two chars "[" "]" is already some parser approach, I'm sure we can do better? - Max
As it's not so easy to make such a function right after all, I wrote one. See answer. - Denys Séguret

1 Answers

1
votes

I wrote a function for you, trying to make it as pretty and reusable as possible :

function setProp(path, newValue, holder) {
    var t = path.split(/[\[\]"]+/).filter(function(v){return v}),
        l = t.pop(), s, o = holder || window;
    while (s = t.shift()) o = o[s];
    o[l] = newValue;
}

You use it like this :

setProp('root["obj1"]["obj2"]', 2);

If your root object isn't in a global variable, pass the relevant holder as third argument.

Demonstration (open the console to see the changed root object)