简体   繁体   English

如何将数组放入另一个数组?

[英]How Can I Array into another Array?

Guys can u help me?伙计们,你能帮帮我吗? I wanna divide an array that is inside an array into another array.我想将一个数组内的数组划分为另一个数组。

For example I need to make it like this:例如,我需要这样做:

var car = [['mazda','red','at'],['honda','blue','at'],['toyota','green','mt']]
var mazda = [] //i want this array contain ['mazda','red','at']
var honda = [] //i want this array contain ['honda','blue','at']
var toyota = [] //i want this array contain ['toyota','green','mt']

Arrays are indexed by number, a positive or null integer. 数组按数字、正整数或空整数进行索引。

So the first element is car[0] , the second car[1] , etc.所以第一个元素是car[0] ,第二个car[1]等等。

That's how you would access the Mazda:这就是您访问马自达的方式:

var mazda = car[0]

The elements inside the mazda array would similarly be accessed:马自达数组内的元素将类似地被访问:

var brand = mazda[0]

However, you may want to consider using maps (objects in JavaScript) like:但是,您可能需要考虑使用地图(JavaScript 中的对象),例如:

car = { mazda: { color: 'red', model: 'at' },
        honda: { color: 'blue', model: 'at' },
        toyota: { color: 'green', model: 'mt' } }

Because then you can explicitly access that info:因为这样您就可以明确访问该信息:

var mazda = car.mazda

I think it's also less prone to mistakes.我认为它也不太容易出错。

Of course, if you have have multiple mazdas, hondas, toyotas... then you will probably need an array at the top, still.当然,如果你有多个马自达、本田、丰田……那么你可能仍然需要一个在顶部的数组。 Something like this:像这样的东西:

car = [
         { brand: 'mazda', color: 'red', model: 'at' },
         { brand: 'mazda', color: 'green', model: 'at' },
         { brand: 'mazda', color: 'blue', model: 'at' },
         ...
    ]

You may also want to check into JSON which is very often used to transmit data in browsers.您可能还想检查JSON ,它经常用于在浏览器中传输数据。 It's arrays, objects, values... If you are to program in JavaScript, you'll want to become a master at such.它是数组、对象、值……如果您要使用 JavaScript 编程,您将希望成为这方面的高手。

Well, the literal answer will be那么,字面的答案将是

var mazda = car[0]

and so forth.等等。 That said, this answer really isn't scalable... if you're learning Javascript, look into objects instead.也就是说,这个答案确实不可扩展……如果您正在学习 Javascript,请改为查看对象。 Then you can do something like:然后你可以做这样的事情:

var cars = { mazda: {color: 'red', otherThing: 'at'}, honda: {... so on ...}};
console.log(cars.mazda.color); //will output 'red'

There are a lot of answers provided now.现在提供了很多答案。 My opinion is that we can destructure the array and assign it to the variables like this:我的观点是,我们可以解构数组并将其分配给变量,如下所示:

 var car = [ ['mazda', 'red', 'at'], ['honda', 'blue', 'at'], ['toyota', 'green', 'mt'] ] let [ mazda, honda, toyota ] = car console.log("Mazda: ", mazda) console.log("Honda: ", honda) console.log("Toyota: ", toyota)

Hope it helps :)希望能帮助到你 :)

If you know the index of the outer array that you need, the other answers have provided the solution.如果您知道所需的外部数组的索引,则其他答案已经提供了解决方案。 If you don't know the index, and only know the first element of the inner array that you're looking for, then find is the tool you need:如果您不知道索引,而只知道要查找的内部数组的第一个元素,那么find就是您需要的工具:

 var car = [['mazda','red','at'],['honda','blue','at'],['toyota','green','mt']] var mazda = car.find(([make]) => make === 'mazda'); var honda = car.find(([make]) => make === 'honda'); var toyota = car.find(([make]) => make === 'toyota'); console.log(mazda); console.log(honda); console.log(toyota);

I suggest that you read about JavaScript arrays.我建议你阅读 JavaScript 数组。 To help you get started, you can do one of your examples like this:为了帮助您入门,您可以执行以下示例之一:

var mazda = car[0];

You should also learn about objects.您还应该了解对象。 Usually arrays should be used for a sequence of similar data.通常数组应该用于类似数据的序列。 Here 'mazda' , 'red' , and 'at' are attributes of a car, but the order doesn't matter.这里'mazda''red''at'是汽车的属性,但顺序无关紧要。 'mazda' is the brand, and 'red' is the color. 'mazda'是品牌, 'red'是颜色。 These would make more sense to store in an object.这些将更有意义存储在对象中。

Use the index of the inner arrays when generating new ones.生成新数组时使用内部数组的索引。

var mazda = car[0];
var honda = car[1];
var toyota = car[2];

You can find a lot of good beginner information on Arrays, here .你可以找到关于阵列很多好的初学者的信息, 在这里

This is a basic case of multi-dimensional array.这是多维数组的基本情况。 You might wanna learn and explore more on arrays .您可能想了解和探索更多关于数组的内容

To solve your case you can solve as:要解决您的情况,您可以解决为:

var mazda = car[0];
var honda = car[1];
var toyota = car[2];

This would have been better solved had you considered using objects.如果您考虑使用对象,这会更好地解决。 You can learn more about objects here .您可以在此处了解有关对象的更多信息。

Since you have multiple mazdas structure of data you must consider is :由于您有多个mazdas数据结构,您必须考虑的是:

var mazda = [{color: 'red', model: 'at'}, {color: 'green', model: 'at'}];

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

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