jQuery是用Javascript編寫的,而Javascript本身提供了Array對象。
所以訪問數組的第0個元素是array_name[0]
在你的榜樣,你存儲對象的數組中的元素。您的對象包括一個「索引」屬性,但要小心,您的「索引」屬性與數組中的元素索引無關。你應該不包括 「索引」 屬性...如:
var lotsData = [
{ // this is the first array element, index value in the array is 0
index: 1,
data: 'I want to be in HTML',
},
{ // this is the second array element, index value in the array is 1
index: 0,
data: "I don't want to be in HTML",
}]
lotsData[0].data // value: 'I want to be in HTML'
的更好的例子是:
var lotsData = [
{ // this is the first array element, index value in the array is 0
category: 'shoe',
detail: 'red'
},
{ // this is the second array element, index value in the array is 1
category: 'fruit',
detail: 'apple'
}]
lotsData[0].detail // value: 'red'
補充:尾隨逗號
注意的是,雖然JavaScript是一種強大的語言,它確實有它的怪癖。 一個重要的一個尾隨逗號,如
...
index: 0,
data: "I don't want to be in HTML", // Trailing comma. DON'T DO THIS!
}]
的問題是,後面的逗號不是JavaScript語言的正式組成部分。大多數JS引擎都支持它,但一個非常重要的不支持:Internet Explorer瀏覽器支持尾隨逗號,並且在遇到後會死亡。
由於測試的獨特方式,您的測試應始終包含IE。在IE
我測試7
感謝。我是一個Rubyist索引功能相同的方式 –
@JZ感謝您的支票!我也是Rubyist,因爲我學習越來越多的Javascript而感到高興 - 它是一個功能強大,功能全面的語言。當然,它不是紅寶石,但它比它(老)的聲望讓我相信的能力強得多。 –
@JZ,請注意關於尾隨逗號的答案... –