JS数组 - 如何构建数组

时间:2010-12-03 02:03:44

标签: javascript

    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[0] is undefined,我不能定义这样的数组吗?在为其分配变量之前,是否必须将adList[0]指定为数组?

5 个答案:

答案 0 :(得分:4)

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

adList           -> Array
adList[0]        -> undefined
adList[0]["img"] -> Error, attempting to access property of undefined.

尝试使用数组和对象文字符号来定义它。

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'
    }
}];

或者您可以单独定义adList[0]adList[1]并使用旧代码:

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

答案 1 :(得分:2)

你必须这样做:

adList[0] = new Array();

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

答案 2 :(得分:2)

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

 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'
}];

答案 3 :(得分:1)

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

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的内容为0-4。那么你可以继续分配他们的价值观。

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

答案 4 :(得分:1)

您的问题是adList[0]adList[1]尚未启动。这与adList作为数组无关。

因此,在分配属性值之前adList[0] = new Object();应该适用于您要执行的操作。

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';