我想创建一个多维键控数组。
如何声明数组然后将内容推送到它?
这是对的吗?
var galleryData = new Array();
$("#gallery li.gallery-image-item:not(:first)").each(function() {
galleryData.push({comment: 'comment', youTube: 'ODOIUOIhd'});
}
由于
答案 0 :(得分:0)
如果你想要'keyed'数组,我认为你需要像
这样的东西array['key'] = { comment: 'comment', youtube: 'ODD2345UI' };
答案 1 :(得分:0)
那会有效。另一种语法是
var galleryData = [];
这很好,因为你可以这样做:
var superGalleryData = [[],[],[]]; //creates an array of 3 arrays
另一个答案建议使用关联数组,但通常不是一个好主意: http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/
答案 2 :(得分:0)
以下是我对你的测试:http://jsfiddle.net/neuroflux/MtuLc/1/
var galleryData = [];
$("#gallery li.gallery-image-item:not(:first)").each(function() {
galleryData.push({comment: 'comment', youTube: 'ODOIUOIhd'});
});
请注意,我修复了您丢失的括号,并更改了Array
表示法。我还使用jQuery 只是输出到页面上。