Javascript JS阵列-如何构建阵列

Javascript JS阵列-如何构建阵列,javascript,Javascript,我收到一个错误,adList[0]未定义,我不能这样定义数组吗?在为其分配变量之前,是否必须将adList[0]指定为数组?必须执行以下操作: var adList = new Array(); adList[0]["img"] = 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg';/* adList[0]["h3"] = 'Product title2'; adList[

我收到一个错误,
adList[0]未定义,我不能这样定义数组吗?在为其分配变量之前,是否必须将
adList[0]
指定为数组?

必须执行以下操作:

    var adList = new Array();
    adList[0]["img"] = 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg';/*
    adList[0]["h3"] = 'Product title2';
    adList[0]["button"]["link"] = '#link-url';
    adList[0]["button"]["text"] = 'Buy now';
    adList[0]["h3"] = 'asdfasdfasdf';

    adList[1]["img"] = 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg';
    adList[1]["h3"] = 'Product title2';
    adList[1]["button"]["link"] = '#link-url';
    adList[1]["button"]["text"] = 'Buy now';
    adList[1]["h3"] = 'asdfasdfasdf';

与创建adList本身的方式类似。

y在开始使用它之前,必须指定数组包含的内容。这就像尝试使用任何其他未初始化的变量一样

adList[0] = new Array();
var adList=new Array();
sizeOfArray=5;
对于(变量i=0;i
这将使用数组在0-4处初始化adList的内容。然后您可以继续分配它们的值


请注意,
adList[0][“img”]
实际上并不使用数组的索引。因为数组是一个对象,所以它允许您设置其属性。这与
adList[0]相同。img

问题在于,您试图引用的
adList[0]
尚未指定值。因此,当您尝试访问
adList[0]['img']
的值时,会发生以下情况:

var adList = new Array();
sizeOfArray = 5;
for (var i=0; i < sizeOfArray; i++) {
    adList[i] = new Array();
}

adList[0]["img"] = 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg';
尝试使用数组和对象文字符号来定义它

adList           -> Array
adList[0]        -> undefined
adList[0]["img"] -> Error, attempting to access property of undefined.
或者您可以简单地分别定义
adList[0]
adList[1]
,并使用旧代码:

adList = [{
    img: 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg',
    h3: 'Product title 1',
    button: {
        text: 'Buy now',
        url: '#link-url'
    }
},
{
    img: 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg',
    h3: 'Product title 2',
    button: {
        text: 'Buy now',
        url: '#link-url'
    }
}];

您还可以使用对象表示法:

var adList = new Array();
adList[0] = {};
adList[1] = {};
adList[0]["img"] = 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg';
... etc etc

您的问题是,
adList[0]
adList[1]
尚未初始化。这与
adList
作为数组无关

所以,
adList[0]=newobject()应该适用于您尝试执行的操作

 adList = [
 {
   "img": 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg',
   "h3": 'Product title2',
   "button": {"link": '#link-url',
              "text": 'Buy now'},
   "h3": 'asdfasdfasdf'
 }, {
   "img": 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg',
   "h3": 'Product title2',
   "button": {"link": '#link-url',
              "text": 'Buy now'},
   "h3": 'asdfasdfasdf'
}];

请告诉我:这里有一个JSFIDLE,显示了我将要使用的数组/对象混合的一个实例。
var adList = new Array();
    adList[0] = new Object();
    adList[0]["img"] = 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg';/*
    adList[0]["h3"] = 'Product title2';
    adList[0]["button"]["link"] = '#link-url';
    adList[0]["button"]["text"] = 'Buy now';
    adList[0]["h3"] = 'asdfasdfasdf';

    adList[1] = new Object();
    adList[1]["img"] = 'http://www.gamer-source.com/image/media/screenshot/thumbnail/489.jpg';
    adList[1]["h3"] = 'Product title2';
    adList[1]["button"]["link"] = '#link-url';
    adList[1]["button"]["text"] = 'Buy now';
    adList[1]["h3"] = 'asdfasdfasdf';