使用string中的键编辑多维数组值

时间:2020-11-28 20:13:38

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

我有一些js代码的问题,我希望你能帮助我

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!

1 个解决方案

#1


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>

#1


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>

相关文章