新手问题。我正在将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数组吗?
答案 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);