2010-12-03 87 views
2
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]作爲數組?JS陣列 - 如何構建陣列

回答

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

打敗我吧:P這裏是一個JSFiddle,展示了我將要使用的數組/對象組合的實例。 http://jsfiddle.net/subhaze/A8vmX/ – subhaze 2010-12-03 02:31:07

2

你必須做到:

adList[0] = new Array(); 

以類似的方式你如何創建adList本身。

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

這將用數組初始化0-4的adList的內容。那麼你可以繼續分配它們的值。

請注意,adList[0]["img"]實際上並未使用數組的索引。由於該數組是一個對象,它可以讓你設置它的屬性。這與adList[0].img相同。

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

偉大的思想家都認爲。更高的思想思考得更快:p。 – 2010-12-03 02:16:27

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