0

我对 javascript 中的嵌套对象有疑问。我想生成,比如说,对象树。例如我有这样的树:

var tree = {
    item1: {
        item11: {},
        item12: {}
    },
    item2: {
        item21: {
            item211: {},
            item212: {}
        }
    }
}

现在我在字符串中有路径(如),我item1.item11item111使用.treepath

调用treePush函数后,我想要这个:

var tree = {
    item1: {
        item11: {
            item111: {}
        },
        item12: {}
    },
    item2: {
        item21: {
            item211: {},
            item212: {}
        }
    }
}

现在我有这段代码,但这会将新项目放入根目录中,tree而不是放入指定级别:

//use example: treePush('item1.item11', 'item111', tree);

function treePush(path, value, tree) {
    var branch = getBranch(path, tree);

    branch[value] = {};
    $.extend(tree, branch);

    return tree;
}

function search(key, tree) {
    //searches key in tree and generates path like 'item1.item11'
}

function getBranch(path, tree) {
    var keys = path.split('.'),
        obj = tree,
        branch = {};

    for(var i = 0; i < keys.length - 1; i++) {
        var key = keys[i];

        if (obj[key] === undefined) {
            return {};
        }

        branch[key] = obj[key]; 
        obj = obj[key];
    }

    return branch;
};

我认为问题出在treePush函数(branch[value] = {};)的第 5 行,但我无法使其正常工作。任何帮助表示赞赏。

4

2 回答 2

0

用这个:

Object.byString = function(o, s) {
    s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
    s = s.replace(/^\./, '');           // strip a leading dot
    var a = s.split('.');
    while (a.length) {
        var n = a.shift();
        if (n in o) {
            o = o[n];
        } else {
            return;
        }
    }
    return o;
}


Object.byString(tree, 'item1.item11')['item111'] = {};

jsFiddle 工作示例:http: //jsfiddle.net/ehE8X/

Object.byString这里找到。

于 2013-08-30T13:55:59.600 回答
0

正如您所说,您要选择路径“item1.item11”并将给定的“item111”推入其中。为此,您要么必须指定要推送的键/值,要么只给它一个对象并将其混合在给定的路径中。mixin 方法需要 jQuery 用于 $.extend 方法,我不太喜欢这个方法(依赖 jQuery 来完成这个简单的任务)。

所以这里有两个版本。

jQuery 混合版本

var tree = {
    item1: {
        item11: {},
        item12: {}
    },
    item2: {
        item21: {
            item211: {},
            item212: {}
        }
    }
};

function treePush (tree, path, item) {
    var key, branch = tree;
    path = path.split('.');
    while (path.length) {
        key = path.shift();
        branch = branch[key] = branch[key] || {};
    }
    $.extend(branch, item);
}

treePush(tree, 'item1.item11', {item111: 'value'});
console.log(tree.item1.item11.item111 === 'value');

在路径中指定目标键

var tree = {
    item1: {
        item11: {},
        item12: {}
    },
    item2: {
        item21: {
            item211: {},
            item212: {}
        }
    }
};

function treePush (tree, path, value) {
    var key, branch = tree;
    path = path.split('.');
    while (path.length > 1) {
        key = path.shift();
        branch = branch[key] = branch[key] || {};
    }
    branch[path.shift()] = value;
}

treePush(tree, 'item1.item11.item111', 'value');
console.log(tree.item1.item11.item111 === 'value');
于 2013-08-30T14:04:57.487 回答