Firebug显示Object Array还有1个元素

时间:2017-10-30 15:29:25

标签: jquery

新手问题。我正在将3个对象推入数组:

var objarray = [{}];
objarray.push({"color":"red", "title":"ABC"});
objarray.push({"color":"blue", "title":"DEF"});
objarray.push({"color":"green", "title":"XYZ"});

console.log(objarray);

Firebug显示有4个元素。这只发生在Object数组吗?

enter image description here

1 个答案:

答案 0 :(得分:4)

It shows 4 objects as you create the array with an empty object in it - [{}]. Remove the inner braces and you'll have your three populated objects only:

var objarray = []; // note: removed {}
objarray.push({"color":"red", "title":"ABC"});
objarray.push({"color":"blue", "title":"DEF"});
objarray.push({"color":"green", "title":"XYZ"});

console.log(objarray);