简体   繁体   English

一个数组可以有多个数组吗?

[英]Can an array have more than one array?

Can I make an array and put another array in it? 我可以制作一个数组并放入另一个数组吗? Like: 喜欢:
var cars = [[sportCars], [luxuryCars], [automaticCars]]
Each of the sportCars luxuryCars automaticCars is an unique array and have a unique elemnts. 每辆sportCars luxuryCars automaticCars都是独特的阵列,并且具有独特的元素。

Yes, you can, exactly the way you did in your example. 是的,您可以完全按照示例中的方法进行操作。

Note that if sportCars , et. 请注意,如果sportCars等。 al., is an array, the code in your question is adding an extra level by wrapping it in a second array. 例如,是一个数组,问题中的代码通过将其包装在第二个数组中来添加额外的级别。 There's no need to do that, just: 无需这样做,只需:

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

...gives you an array containing an array: ...给您一个包含数组的数组:

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

You don't need (and probably don't want) the extra [ and ] around sportCars that you have in your question. 您不需要(也可能不需要)您在问题中遇到的sportCars周围的多余[]

You can do it like this: 您可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM