Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过点表示法访问对象属性_Javascript - Fatal编程技术网

Javascript 通过点表示法访问对象属性

Javascript 通过点表示法访问对象属性,javascript,Javascript,我正在迭代从服务器返回的集合;看起来是这样的: roster: Array 0: Object avatar: null contactName: "me@test.com" contactType: "grouping" displayName: "Joe Shmoe" 我正在创建一个表,并试图将“displayName”添加到其中,但通过点表示法进行访问不起作用。我下面的代码有什么问题 function createA

我正在迭代从服务器返回的集合;看起来是这样的:

roster: Array
    0: Object
        avatar: null
        contactName: "me@test.com"
        contactType: "grouping"
        displayName: "Joe Shmoe"
我正在创建一个表,并试图将“displayName”添加到其中,但通过点表示法进行访问不起作用。我下面的代码有什么问题

function createAddressBook()
            {
                var tbl = document.getElementById( 'addressBook_tbl' );

                var tbdy = document.createElement( 'tbody' );

                // cells creation
                for( var j = 0; j <= roster.length; j++ ) 
                {
                    // table row creation
                    var row = document.createElement( "tr" );

                    for( var i = 0; i < 2; i++ ) 
                    {
                        // create element <td> and text node 
                        //Make text node the contents of <td> element
                        // put <td> at end of the table row
                        var cell = document.createElement( "td" );    
                        var cellText = document.createTextNode( roster[ j ].displayName ); 

                        cell.appendChild( cellText );
                        row.appendChild( cell );
                    }

                    //row added to end of table body
                    tbdy.appendChild( row );
                }

                // append the <tbody> inside the <table>
                tbl.appendChild( tbdy );
            }
函数createAddressBook()
{
var tbl=document.getElementById('addressBook_tbl');
var tbdy=document.createElement('tbody');
//细胞生成

对于(var j=0;j您在定义
i
时使用的
j

// ----------------v-------should be `i`
"user: " + roster[ j ].displayName

仅供参考,您可以使用
.insertCell(-1)
附加新单元格

row.insertCell(-1)
   .appendChild(document.createTextNode( "user: " + roster[ j ].displayName )); 

编辑:当更新的代码运行时,它确实有一个错误


您试图在超出上次索引的索引处访问
花名册
。由于数组索引是基于0的,所以上次索引是
花名册.length-1
,因此在定义
i
时,您应该使用
您正在使用的
j

// ----------------v-------should be `i`
"user: " + roster[ j ].displayName

仅供参考,您可以使用
.insertCell(-1)
附加新单元格

row.insertCell(-1)
   .appendChild(document.createTextNode( "user: " + roster[ j ].displayName )); 

编辑:当更新的代码运行时,它确实有一个错误


您试图在超出上一个索引的索引处访问
花名册
。因为数组索引是基于0的,所以上一个索引是
花名册.length-1
,因此您应该使用
,我打赌它也不能使用方括号表示法,因为您没有使用
i
访问它,我打赌它不能使用square括号表示法也因为您没有使用
i
访问它,所以只更新了我的原始帖子,因为它没有显示带有“J”的外部循环定义。那么你的代码对我来说就很好。@fumeng:你的代码中有一个错误在
花名册
循环结束之前不会显示出来。我更新了我的答案。只要我修复了@fumeng:不客气。我想
tbdy
在循环过程中肯定不在DOM中。对不起,刚刚更新了我的原始帖子se它没有显示定义了“J”的外部循环。那么你的代码对我来说运行得很好。@fumeng:你的代码中有一个错误在
花名册
循环结束之前不会显示出来。我更新了我的答案。只要我修复了@fumeng:不客气。我假设
tbdy
在循环过程中肯定不在DOM中。