0

I have a problem with some js code and i hope you will help me

I've got an array like this :

var object = {
    one : {
        'first' : 'value1',
        'second' : 'value2'
        'third' : {
            'first' : 'value1',
            'second' : 'value2'
        }
    }
    ...
}

And a string like :

var string = 'one.third.second'

So, my question is - how can i edit the object's value with key from the string?

Thanks!

radoslav55
  • 5
  • 1
  • 4
  • @T.J. Crowder, while the linked answers shows how to get an object property given a string, they do not show how to change the value – AmmarCSE Nov 26 '15 at 17:03
  • Also, frankly, I cannot think of a way to do it without `eval` :) – AmmarCSE Nov 26 '15 at 17:07
  • @AmmarCSE: Good point. I just recently answered *that* question, too, let me find it... – T.J. Crowder Nov 26 '15 at 17:38
  • @AmmarCSE: Gah, I can't find it. It'll be somewhere in my last 2-3 weeks of answers, but I'm out of time to search. (You don't need `eval`. You almost never do...) – T.J. Crowder Nov 26 '15 at 17:49
  • @T.J.Crowder, eh, thats fine. Actually, using brackets and the knowledge of the number of keys, you can do `obj[keys[0]][keys[1]][keys[2]] = 'whatever'`, but still, thats going to be inefficient for a large number of keys. Im liking thinking this through, it really tests the limits of my (little) js knowledge :) – AmmarCSE Nov 26 '15 at 17:55
  • 1
    @AmmarCSE: You do it by adapting the function in this [answer](http://stackoverflow.com/a/6491621/157247) so you're assigning at the end. It's a relatively small change... Go for it! You can do it! – T.J. Crowder Nov 26 '15 at 17:57
  • I didn't know for the eval() function. However, it helped me a lot! Thank you! – radoslav55 Nov 26 '15 at 18:21
  • @radoslav55: Don't use `eval` for this, it's overkill and can be tricky. Be careful using `eval` anyway (only ever use it with your own code, for instance). – T.J. Crowder Nov 26 '15 at 18:42

1 Answers1

0

Well, I sure can't find my previous answer on this, maybe it was my imagination.

You do it by using the technique in this answer, but remembering the last object and doing an assignment when you get to the last key instead of getting the value:

Object.setByString = function(o, s, value) {
    s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
    s = s.replace(/^\./, '');           // strip a leading dot
    var a = s.split('.');
    for (var i = 0, n = a.length - 1; i < n; ++i) {
        var k = a[i];
        if (k in o) {
            o = o[k];
        } else {
            return;
        }
    }
    o[a[a.length - 1]] = value;
};

var obj = {
    one : {
        'first' : 'value1',
        'second' : 'value2',
        'third' : {
            'first' : 'value1',
            'second' : 'value2'
        }
    }
};

var string = 'one.third.second';

Object.setByString(obj, string, 42);
snippet.log(JSON.stringify(obj, null, 2));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875