如何在javascript中打印对象迭代的子对象?

时间:2018-04-03 19:14:58

标签: javascript object string-concatenation console.log string-conversion

我有对象:

[Object]
    0: {Class: "MTH 1100-A", __rowNum__: 1}
    1: {Class: "CSC 3200-B", __rowNum__: 2}
    2: {Class: "ART 4334-E", __rowNum__: 3}
    3: {Class: "REC 3223-C", __rowNum__: 4}

在我这样做之前,我想把所有这些都放到一个数组中,虽然我只是在尝试打印它们,但我似乎无法做到这一点。这是我的代码:

const obj = {
  0: { Class: "MTH 1100-A",__rowNum__: 1},
  1: { Class: "CSC 3200-B", __rowNum__: 2 },
  2: { Class: "ART 4334-E", _rowNum__: 3 } ,
  3: { Class: "REC 3223-C", _rowNum__: 4 }
};

function getDescendantProp(obj, desc) {
  var arr = desc.split(".");
  while (arr.length && (obj = obj[arr.shift()]));
  return obj;
}

console.log(getDescendantProp(obj, '0.Class'));

for (var i = 0; i < obj.length; i++) {
  console.log(getDescendantProp(obj, "\'" + i + ".Class\'"));
}

对于输出,你得到:

“MTH 1100-A”
未定义
未定义
未定义
未定义

那么为什么我不能得到那些价值.....?

3 个答案:

答案 0 :(得分:0)

您的代码存在一些问题。

  1. 您打印的行属于第一个console.log()调用。在那里你传递了0.Class&#39;,你不会在for循环内的console.log()调用上做。在那里,这应该足够getDescendantProp(obj, i + ".Class")。无需逃避报价。
  2. 你的while循环中的状况对我来说似乎很奇怪。通过使用=运算符,您可以将表达式的右侧分配给obj,从而用obj [arr.shift()]替换obj的原始值。如果obj [arr.shift()]恰好不为null,则会在while循环的第一次迭代中发生,并且条件通过。然后你返回obj,它已成为你正在寻找的价值。我不得不说,我发现找到你正在寻找的对象属性是一种巧妙的方法,但有点过度设计。对于比较,您应该使用==或===。
  3. 我修改了一下你的代码以便它可以工作并添加了一些变量来跟踪发生了什么:

        var obj = {
            0: {Class: "MTH 1100-A", __rowNum__: 1},
            1: {Class: "CSC 3200-B", __rowNum__: 2},
            2: {Class: "ART 4334-E", __rowNum__: 3},
            3: {Class: "REC 3223-C", __rowNum__: 4}
        }
    
        function getDescendantProp(obj, desc) {
            var arr = desc.split(".");
            var arrCopy = arr.slice();
            arrCopy = arrCopy.shift();
            while(arr.length && (obj = obj[arr.shift()])){
                var bla = "bla";
            };
            return obj;
        }
    
        for(var i = 0; i< Object.keys(obj).length; i++) {
            var o = getDescendantProp(obj,  i + ".Class");
            console.log(o);
        }
    

    但是,请注意,您可以像obj.property或obj [&#34; property&#34;]一样访问对象属性。考虑到这一点,可以进一步简化为以下内容:

        var descendantsArray = [];          // Array to put each desired element from obj
        var objKeys = Object.keys(obj);     // Array with the keys to the object
    
        for (var i = 0; i < objKeys.length; i++) {
            var currentKey = objKeys[i];                    // Get current key
            var currentElement = obj[currentKey];           // Get the element that corresponds to the key
            descendantsArray.push(currentElement.Class);    // Add the current element's Class property to the Array
            console.log(currentElement.Class);              // Print the 'Class' property of the current element
        }
    

    干杯

答案 1 :(得分:0)

对象没有length属性。所以你循环虽然未定义了值。

您可以将对象转换为Object.values(obj)的数组:

&#13;
&#13;
const obj = {
  0: { Class: "MTH 1100-A",__rowNum__: 1},
  1: { Class: "CSC 3200-B", __rowNum__: 2 },
  2: { Class: "ART 4334-E", _rowNum__: 3 } ,
  3: { Class: "REC 3223-C", _rowNum__: 4 }
};

function getDescendantProp(obj, key) {
  return Object.values(obj).map(i => i[key]);
}

console.log(getDescendantProp(obj, 'Class'));

const arr = getDescendantProp(obj, 'Class');
arr.forEach(i => {
  console.log(i);
});
&#13;
&#13;
&#13;

答案 2 :(得分:0)

我找到了答案,一个小的语法错误,我不明白,但这里是问题:

for (var i = 0; i < obj.length; i++) {
  console.log(getDescendantProp(obj, "\'" + i + ".Class\'"));
}

for (var i = 0; i < obj.length; i++) {
  console.log(getDescendantProp(obj, i+`.Class`));
}