自动化嵌套对象 [英] Automated nested objects

查看:142
本文介绍了自动化嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,当我想知道是否可以自动进行对象嵌套时,我在玩一些js.当然我还是个新手,所以我还没走太远.

A couple of days ago I was having fun with some js, when I came to the question if I could automate object nesting. Of course I'm still a newby so I haven't gotten too far.

但是我得到的是这个

var a = {};
var stso = ""; storing the second object

function sto(b) { // start the object
    a[b] = {};
    stso = b;
}

function nmo(...objs) { // nesting more object
    console.log(objs[0]);
    if(objs.length) { // checking to see that I have at least one variable before proceding
        for(i = 0; i < objs.length; i++) { // looping through arguments
            a[stso][objs[i]] = {}; // and now I would have to repeat one more more for lever for every argument, meaning, the deeper I want to go into the object, the more nested for loops I have to make.
        }
    }
}

sto("b");
nmo("c");
a.b.c = "Happy ending!";

console.log(a.b.c); // It seems we still dont have a happy ending

// and as a second example

sto("b");
nmo("c", "d", "e", "f", "g");
a.b.c.d.e.f.g = "Another happy ending!";

console.log(a.b.c.d.e.f.g); // Our second happy ending was also unhappy...

总而言之,您在一个函数中定义了第二个对象,然后在第二个函数中依次定义了所需的对象.

In summary, you define the second object in one function, you define as many objects as you want in your second function in order.

如何用当前的结构实现这一目标?

How could I achieve this with my current structure?

推荐答案

如果我对您的理解正确,则可以执行以下操作:

If i understand you correctly, you can do something like this:

var createNestedObjects = function( obj ) {
    //get all the passed arguments 
    var args = Array.prototype.slice.call(arguments);

    //start with i = 1, as we want to skip obj in the arguments
    for( var i = 1; i < args.length; i++ ) {
        obj = obj[ args[i] ] = obj[ args[i] ] || {};
    }

};

var a = {};
createNestedObjects( a, "b", "c", "d", "e", "f" );
a.b.c.d.e.f = "happy ending";
console.log(a.b.c.d.e.f); //logs "happy ending"


说明行3

您的要求是将所需数量的字符串传递给函数,以创建所需大小的嵌套对象. 但是,如您所见,该函数只有一个参数:obj. 很棒的事情是javascript允许您传递更多的参数,并且仍然可以使用arguments对象访问它们. arguments对象在所有函数中均可用. arguments对象类似于数组,但与其本身是一个对象并不完全相同,如果在这种情况下将其记录下来,它将显示:

Your requirement is to pass as many strings as required to the function to create any size of nested objects you would like. However, as you can see, the function only has one parameter: obj. The cool thing is that javascript allows you to pass even more parameters and you are still able to access them using the arguments object. The arguments object is available in all functions. The arguments object is similar to an array, but not quite the same as it is an object in it's own, if you log it in this case it will display:

Arguments [{}, "b", "c", "d", "e", "f"] (6) 

我们无法使用for循环遍历agruments对象,因此在第3行将其首先转换为数组. 参数对象引用

We can't loop through the agruments object using a for loop, so on line 3 it's converted to an array first. arguments object reference

循环内的解释

此行中有两个有趣的部分. Javascript允许您一次分配多个变量,因为赋值运算符会根据其右操作数的值为其左操作数分配一个值

There are two interesting parts in this line. Javascript allows you to assign multiple variables at once as the assignment operator assigns a value to its left operand based on the value of its right operand

  var a, b;
  a =  b = 10; //both a and b are set to 10

使用||如果左侧的值未定义,则在此处使用operator设置默认值(在这种情况下为{}).例如,设置默认值也很方便

Using || operator is used here to set a default value ( in this case {} )if the value on the left is undefined. Which is for example also handy to set default values

function setDelay ( delay ){

    delay = delay || 60
    return delay

}

setDelay(  ) //no parameter is passed, delay is set to 60 
setDelay( 120 ) 120 is passed and delay is set to 120

在这种情况下,行

obj = obj[ args[i] ] = obj[ args[i] ] || {};

可以改写为:

var name = args[i];    // first loop args[i] is "b"    
if(!obj[name]){        // a.b does not exist       
    obj[name] = {};    // a.b is set to {}      
}
obj = obj[name];       // a is set to a.b 

它检查是否已经存在具有该名称的对象,如果没有,则将其创建并设置为obj,以便我们可以在下一个循环中嵌套对象.

which checks if there is already an object with that name, if not it's created and set as obj so we can nest objects in the next loop.

我希望这可以澄清代码

这篇关于自动化嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆