Getting an Objects nested value from an array of n

2019-03-05 12:15发布

问题:

I have a situation where I need to zip two Objects together retaining both of the values. I can iterate through both the objects and build an array of all the keys.

    var traverse = function (obj, chain) {

        for (var prop in obj) {
            if (obj.hasOwnProperty(prop)) {

                var tempChain = [].concat(chain || []);
                tempChain.push(prop);

                if (typeof obj[prop] === 'object') {
                    traverse(obj[prop], tempChain);
                }
                console.log(tempChain);
            }
        }
    };

Passing in the following:

    traverse({
        'a': {
            'b': 'hello world',
            'b1': 'hello world 1',
            'c': {
                'd': 'dello'
            }
        }
    })

Will return me:

[a]
[a,b]
[a,b1]
[a,c]
[a, c, d]

So I now have an array of nested properties in an object. How can I access essentially obj[[a,c,d]]? I know I can solve the problem through eval but I can't trust the content.

eval('window.' + ['a','c','d'].join('.')); 

If I can loop through that array and check to see if the property exists in both of them, then build a new object of the combined 'zipped' values.

回答1:

Perhaps something like this?

function getValueAt(obj, keyPathArray) {
    var emptyObj = {};

    return keyPathArray.reduce(function (o, key) {
        return (o || emptyObj)[key];
    }, obj);
}

Then you can use it like:

var o = { a: { c: { d: 1 } } };

getValueAt(o, ['a', 'c', 'd']); //1

However it's not efficient for non-existing properties, since it will not short-circuit. Here's another approach without using reduce:

function getValueAt(o, keyPathArray) {
    var i = 0, 
        len = keyPathArray.length;

    while (o != null && i < len) o = o[keyPathArray[i++]];

    return o;    
}