从多维数组中提取值

时间:2021-01-08 21:32:48

I have a very big array with car makes and models. I've already extracted the makes into a separate array, but I am struggling to extract the models while also maintaining their association to the make.

我有一个非常大的汽车品牌和模型阵列。我已经将制作提取到一个单独的数组中,但我正在努力提取模型,同时保持它们与make的关联。

Here is a sample of the array:

这是一个数组示例:

var dataa = new Array
        (
            ['Acura','','Integra','Mdx','Rl','Rsx','Slx','Tl','Tsx'],
            ['Aixam','','400','505','600'],
            ['Alfa romeo','','145','146','147','155','156'],
            ['Aston martin','','.','DBS','Db7','Db9']);

As you can see I have a multi-dimensional array with the car make (located at dataa[0][0]), then an empty value and then the model for this make.

如你所见,我有一个带有汽车品牌的多维数组(位于dataa [0] [0]),然后是一个空值,然后是这个品牌的模型。

I am using this code to to get the car makes:

我正在使用此代码来使汽车制造:

This gives me the fist value of every nested array -> dataa[i][0]:

这给了我每个嵌套数组的第一个值 - > dataa [i] [0]:

for (var i = 0; i < dataa.length; i++) {
  document.write(dataa[i][0] + "<br>");
}

My problems start HERE.

我的问题从这里开始。

I CAN NOT extract all models and assign them to the proper car make. I have tried for-loop's, loops with brakes, while loops and loops with conditional statements but I can't do it.

我无法提取所有模型并将它们分配给正确的汽车制造商。我试过for-loop的,带刹车的循环,而循环和循环有条件语句,但我不能这样做。

Please give me some advice here. Would jQuery or some other technology help me?

请在这里给我一些建议。 jQuery或其他一些技术会帮助我吗?

1 个解决方案

#1


2  

Put a loop inside your loop.

在循环中放置一个循环。

for (var i = 0; i < dataa.length; i++) {
    document.write("<h2>Starting new inner loop!</h2><br>");
    for (var j = 0; j < dataa[i].length; j++) {
        document.write(dataa[i][j] + "<br>");
    }
}

Now for every Array in the outer Array, you're doing a separate loop.

现在,对于外部Array中的每个Array,您都在进行单独的循环。

Here's a demo

这是一个演示

#1


2  

Put a loop inside your loop.

在循环中放置一个循环。

for (var i = 0; i < dataa.length; i++) {
    document.write("<h2>Starting new inner loop!</h2><br>");
    for (var j = 0; j < dataa[i].length; j++) {
        document.write(dataa[i][j] + "<br>");
    }
}

Now for every Array in the outer Array, you're doing a separate loop.

现在,对于外部Array中的每个Array,您都在进行单独的循环。

Here's a demo

这是一个演示