在循环中创建一个深度(水平)对象

发布于 2024-11-01 03:07:53 字数 444 浏览 1 评论 0原文

我不确定表达这个问题的最佳方式是什么,但基本上我希望创建一个循环来创建如下对象:

var dictionary = {};
var arr = [
              ["for", "item", "in", "list"],
              ["if", "condition"]
          ];

// Insert Magic Loop to yield:

dictionary.for.item.in.list // {} (exists, as well as rest of chain)
dictionary.if.condition     // {} (exists, as well as rest of chain)
dictionary.for.item         // {}
dictionary.test             // undefined

I wasn't sure what the best way to word this question is but basically I'm looking to create a loop that create objects like this:

var dictionary = {};
var arr = [
              ["for", "item", "in", "list"],
              ["if", "condition"]
          ];

// Insert Magic Loop to yield:

dictionary.for.item.in.list // {} (exists, as well as rest of chain)
dictionary.if.condition     // {} (exists, as well as rest of chain)
dictionary.for.item         // {}
dictionary.test             // undefined

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离不开的别离 2024-11-08 03:07:53

我的脑海中浮现出:

function create(arrArray) {
   for(var i = 0; i < arrArray.length; i++) {
       var arr = arrArray[i];

       var _dict = dictionary;

       for(var j = 0; j < arr.length; j++) {
           if(!_dict[arr[j]]) {
              _dict[arr[j]] = {};
           }
           _dict = _dict[arr[j]];
       } 
   }
}

编辑

这是解决方案的一个子集。我写了一个函数,它接受一个数组和一个字典作为参数:

function create(arr, dict) {           
    var _dict = dict;

    for(var j = 0; j < arr.length; j++) {
        if(!_dict[arr[j]]) {
           _dict[arr[j]] = {};
        }
        _dict = _dict[arr[j]];
    } 
}

var dictionary = {};

create(["for", "item", "in", "list"], dictionary);
create(["if", "condition"], dictionary)

如果你不担心覆盖,你可以去掉循环中的 if

function create(arr, dict) {           
    var _dict = dict;

    for(var j = 0; j < arr.length; j++) {
        _dict[arr[j]] = {};
        _dict = _dict[arr[j]];
    } 
}

并且为了更好的可读性:

function create(arr, dict) {           
    var _dict = dict;

    for(var j = 0; j < arr.length; j++) {
        var key = arr[j];
        _dict[key] = {};
        _dict = _dict[key];
    } 
}

Off the top of my head:

function create(arrArray) {
   for(var i = 0; i < arrArray.length; i++) {
       var arr = arrArray[i];

       var _dict = dictionary;

       for(var j = 0; j < arr.length; j++) {
           if(!_dict[arr[j]]) {
              _dict[arr[j]] = {};
           }
           _dict = _dict[arr[j]];
       } 
   }
}

EDIT

Here is a subset of the solution. I wrote a function that takes in an array and a dictionary as parameters:

function create(arr, dict) {           
    var _dict = dict;

    for(var j = 0; j < arr.length; j++) {
        if(!_dict[arr[j]]) {
           _dict[arr[j]] = {};
        }
        _dict = _dict[arr[j]];
    } 
}

var dictionary = {};

create(["for", "item", "in", "list"], dictionary);
create(["if", "condition"], dictionary)

If you're not worried about overwriting, you can take out the if in the loop:

function create(arr, dict) {           
    var _dict = dict;

    for(var j = 0; j < arr.length; j++) {
        _dict[arr[j]] = {};
        _dict = _dict[arr[j]];
    } 
}

And for better readability:

function create(arr, dict) {           
    var _dict = dict;

    for(var j = 0; j < arr.length; j++) {
        var key = arr[j];
        _dict[key] = {};
        _dict = _dict[key];
    } 
}
撞了怀 2024-11-08 03:07:53
var dictionary = {};
var arr = [
              ["for", "item", "in", "list"],
              ["if", "condition"]
          ];
for(var i in arr){
    var _i = arr[i];
    var x = dictionary;
    for(var ii in _i){
        x[_i[ii]] = {};
        x = x[_i[ii]];
    }
}
console.log(dictionary);
console.log(dictionary.for.item.in.list);
console.log(dictionary.if.condition );
console.log(dictionary.for.item);
console.log(dictionary.test );
var dictionary = {};
var arr = [
              ["for", "item", "in", "list"],
              ["if", "condition"]
          ];
for(var i in arr){
    var _i = arr[i];
    var x = dictionary;
    for(var ii in _i){
        x[_i[ii]] = {};
        x = x[_i[ii]];
    }
}
console.log(dictionary);
console.log(dictionary.for.item.in.list);
console.log(dictionary.if.condition );
console.log(dictionary.for.item);
console.log(dictionary.test );
一向肩并 2024-11-08 03:07:53

这将需要一些修改(每个序列从顶部重新启动),但显示了降序向下的总体思路:

var arr = ["foo", "if", "then", "bar"]
var dictionary = {}
var obj = dictionary // keep original
for (var i = 0; i < arr.length; i++) {
   var key = arr[i]
   // assign and "move to next".
   // this could be done as `obj = obj[key] = {}`
   obj[key] = {}
   obj = obj[key]
}

快乐编码

This will need some modification (restart at top for each sequence), but shows the general idea of descending-down:

var arr = ["foo", "if", "then", "bar"]
var dictionary = {}
var obj = dictionary // keep original
for (var i = 0; i < arr.length; i++) {
   var key = arr[i]
   // assign and "move to next".
   // this could be done as `obj = obj[key] = {}`
   obj[key] = {}
   obj = obj[key]
}

Happy coding

花开浅夏 2024-11-08 03:07:53
var dictionary = {},
    arr = [
          ["for", "item", "in", "list"],
          ["if", "condition"]
    ];

// loop the outer array
for (var x = 0; x < arr.length; x++) {
    var current = dictionary, // set "current" to the top-level object (dictionary)
        sub = arr[x];     // reference to sub-item

    // loop sub-list
    for (var y = 0; y < sub.length; y++) {
        // create the new object on the "current" object,
        // then assign that new object to be "current"
        current = current[sub[y]] = {};
    }
}

console.log(dictionary);
var dictionary = {},
    arr = [
          ["for", "item", "in", "list"],
          ["if", "condition"]
    ];

// loop the outer array
for (var x = 0; x < arr.length; x++) {
    var current = dictionary, // set "current" to the top-level object (dictionary)
        sub = arr[x];     // reference to sub-item

    // loop sub-list
    for (var y = 0; y < sub.length; y++) {
        // create the new object on the "current" object,
        // then assign that new object to be "current"
        current = current[sub[y]] = {};
    }
}

console.log(dictionary);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文