我無法理解如何調用特定的數組值:
我已經註釋掉了代碼中的問題。請看一看,讓我知道爲什麼數組在該函數內產生一個結果,而在其外部產生不同的結果。要運行代碼,請使用一個網站就像repl.itJavaScript在兩種不同的上下文中調用數組值
var passengers = [ ["Thomas", "Meeks"],
["Gregg", "Pollack"],
["Christine", "Wong"],
["Dan", "McGaw"] ];
var results = passengers.map(function (array) {
// The following prints out the first names--the entire first column. Why?
console.log(array[0]);
});
console.log(); // Just empty space
// Why is the following only printin the first row of passengers (which it should), but the array[0] printed out the entirety of the first column?
console.log(passengers[0]);
因爲在第一個'console.log',你所訪問每個嵌套陣列的第一個成員。你對此有什麼期望? 'array'參數代表'乘客[0]'然後'乘客[1]''''乘客[2]'等等,所以'array [0]'是'passengers [0] [0]'然後'乘客[1] [0]'...... –