访问嵌套对象内的数据

时间:2020-04-15 02:51:01

标签: javascript

我有一个需要访问内部信息的对象,并且已经访问了第一层,但是第二层给了我一个问题。该对象如下所示:

Array
(
    [students] => Array
        (
            [0] => Array
                (
                    [id] => 123
                    [first] => Dave
                    [grades] => Array
                        (
                            [0] => Array
                                (
                                    [class] => Math
                                    [score] => 10
                                )

                            [1] => Array
                                (
                                    [class] =>Science
                                    [score] => 5
                                )

                        )

                )
)

我看到了一条帖子,可以帮助我访问大部分信息。

function createRows(data){
    for(var i in data.students){
      var row = table.insertRow(table.rows.length);
            var cell1 = row.insertCell(0);
            cell1.innerHTML = data.orders[i].id;
            cell1.setAttribute("class", "row");
            var cell2 = row.insertCell(1);
            cell2.innerHTML = data.orders[i].first;
            cell2.setAttribute("class", "row");

            for(var j in data.students[i].grades){
              console.log(data.students[i].grades[j]);
         }
    }
}

第二个for循环给我这个结果

{class: "Math", Score: 10}
{class: "Science", Score: 5}

以及在控制台中打印每个项目后出现的奇怪错误。

 ƒ (){return r}
ƒ Array() { [native code] }
 ƒ (t,e){return Array.forEach(this,t,e),this}
 ƒ (){for(var t=this.length,e=new Array(t);t--;)e[t]=d(this[t]);return e}
 ƒ (){return this.filter(function(t){return null!=t})}
 (t){var e=Array.slice(arguments,1);return this.map(function(n){return n[t].apply(n,e)})}
 ƒ (t){for(var e={},n=Math.min(this.length,t.length),r=0;r<n;r++)e[t[r]]=this[r];return e}
 ƒ (t){for(var e={},n=0,r=this.length;n<r;n++)for(var i in t)if(t[i](this[n])){e[i]=this[n],delete t[i];break}return e}
 ƒ (t,e){return-1!=this.indexOf(t,e)}
 ƒ (t){return this.push.apply(this,t),this}
 ƒ (){return this.length?this[this.length-1]:null}
f (){return this.length?this[Number.random(0,this.length-1)]:null}
 ƒ (t){return this.contains(t)||this.push(t),this}
 ƒ (t){for(var e=0,n=t.length;e<n;e++)this.include(t[e]);return this}
 ƒ (t){for(var e=this.length;e--;)this[e]===t&&this.splice(e,1);return this}
 ƒ (){return this.length=0,this}
 ƒ (){for(var t=[],e=0,n=this.length;e<n;e++){var r=typeOf(this[e]);"null"!=r&&(t=t.concat("array"==r||"collection"==r||"arguments"==r||instanceOf(this[e],Array)?Array.flatten(this[e]):this[e]))}return …
...

0 个答案:

没有答案