2014-02-06 63 views
-1

我可以製作一個數組並放入另一個數組嗎?像:
var cars = [[sportCars], [luxuryCars], [automaticCars]]
sportCarsluxuryCarsautomaticCars是一個獨特的陣列,並具有獨特的elemnts。數組可以有多個數組嗎?

+2

是的,你可以做到這一點。 – jonhopkins

+4

你爲什麼不試試看? –

回答

2

是的,你可以,就像你在你的例子中一樣。

請注意,如果sportCars,等。 al。是一個數組,你問題中的代碼通過將它包裝到第二個數組中來添加一個額外的級別。有沒有必要那麼做,只是:

var sportCars = ["Ferrari", "Lamborghini"]; 
var cars = [sportCars]; 

...給你包含數組的數組:

console.log(cars[0][0]); // "Ferrari" 

你並不需要(也可能不希望)額外[]sportCars你在你的問題。

0

你可以這樣說:

var sportCars = ['car1', 'car2']; 
var luxuryCars = ['car3']; 
var automaticCars = ['car4', 'car5']; 
var cars = [sportCars, luxuryCars, automaticCars]; 
相關問題