简体   繁体   English

通过嵌套$ .each遍历对象

[英]Loop through object with nested $.each

I'm having trouble looping through an object using a nested $.each. 我在使用嵌套的$ .each遍历对象时遇到麻烦。 The object is a series of object of the same type/class nested under rootObject. 该对象是嵌套在rootObject下的相同类型/类的一系列对象。

The object 物体

var rootObject ={};

rootObject.reportObject1.name = "reportObject1 Name";
rootObject.reportObject1.prop1 = "reportObject1 Prop1_Value";
rootObject.reportObject1.prop2 = "reportObject1 Prop2_Value";

rootObject.reportObject1.reportObjectA.name = "reportObjectA Name";
rootObject.reportObject1.reportObjectA.prop1 = "reportObjectA Prop1_Value";
rootObject.reportObject1.reportObjectA.prop2 = "reportObjectA Prop2_Value";

The Loop 循环

$.each(rootObject, function(index0, value0){
  console.log(value0.name);

  $.each(value0, function(index1, value1){
    console.log(value1.name);
  }

}

The Problem 问题

  1. value1.name is returning object properties other than name in the loop. value1.name返回的是循环中name以外的对象属性。 It's seemingly attempting to return value.name for prop1 & prop2 , resulting in "undefined" values. 似乎正在尝试返回prop1prop2 ,从而导致“未定义”值。

  2. When looking into the value of value0 during debugging, value0 appears to loose its value as it enters the nested loop. 当寻找到的值value0调试期间, value0出现在进入嵌套循环失去它的值。 Ie at console.log(value1.name) , value0 , from the parent loop, becomes undefined ; 即从父循环的console.log(value1.name) value0变为undefined

  3. When looking into the child loop ( index1, value1 ) during debugging, I see that value1 now equals value0.name , and index1 equals "name". 在调试过程中查看子循环( index1, value1 )时,我看到value1现在等于value0.name ,而index1等于“ name”。

You can automatically define properties one level deep, but for two you need to stop and instantiate the parent: 您可以自动定义一个深度的属性,但是对于两个属性,您需要停止并实例化父级:

var rootObject ={};

rootObject.reportObject1 = {}; // HERE
rootObject.reportObject1.name = "reportObject1 Name";
rootObject.reportObject1.prop1 = "reportObject1 Prop1_Value";
rootObject.reportObject1.prop2 = "reportObject1 Prop2_Value";

rootObject.reportObject1.reportObjectA = {}; // and HERE
rootObject.reportObject1.reportObjectA.name = "reportObjectA Name";
rootObject.reportObject1.reportObjectA.prop1 = "reportObjectA Prop1_Value";
rootObject.reportObject1.reportObjectA.prop2 = "reportObjectA Prop2_Value";

Without that, none of these properties are actually getting defined, leading to your undefined results. 没有这些,这些属性实际上都不会被定义,从而导致undefined结果。

The next issue is another syntax problem: you're missing closing parentheses on the two $.each() calls: 下一个问题是另一个语法问题:您在两个$.each()调用上缺少$.each()括号:

$.each(rootObject, function(index0, value0){
  console.log(value0.name);

  $.each(value0, function(index1, value1){
    console.log(value1.name);
  }); // HERE

}); // and HERE

With these two fixes, your console output shows: 有了这两个修复程序,您的控制台输出将显示:

reportObject1 Name
undefined (x3)
reportObjectA Name

To get the correct output, or at least some sample output, you could use this little gem ( from here ). 为了获得正确的输出或至少一些示例输出,您可以使用这个小宝石( 从此处开始 )。 Because your structure could potentially have more than two levels, recursion seems appropriate here. 因为您的结构可能具有两个以上的级别,所以在这里递归似乎很合适。

function enumerate(o,s){

    //if s isn't defined, set it to an empty string
    s = typeof s !== 'undefined' ? s : "";

    //iterate across o, passing keys as k and values as v
    $.each(o, function(k,v){

        //if v has nested depth
        if(typeof v == "object"){

            //write the key to the console
            console.log(s+k+": ");

            //recursively call enumerate on the nested properties
            enumerate(v,s+"  ");

        } else {

            //log the key & value
            console.log(s+k+": "+String(v));
        }
    });
}

If you try enumerate(rootObject) , you will get: 如果尝试enumerate(rootObject) ,将得到:

reportObject1: 
   name: reportObject1 Name
   prop1: reportObject1 Prop1_Value
   prop2: reportObject1 Prop2_Value
   reportObjectA: 
     name: reportObjectA Name
     prop1: reportObjectA Prop1_Value
     prop2: reportObjectA Prop2_Value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM