JavaScript Add Values to Multidimensional Array - Stack Overflow

Hi I am trying to add some values to a multidimensional array using a for loop. This is what I have cre

Hi I am trying to add some values to a multidimensional array using a for loop. This is what I have created so far:

var test1 = [];
var test2 = [];
for (var i = 0; i < top10.length; i++)
{
    test1[i] = i;
    test1[i][0] = top10[i][0];
    test1[i][1] = top10[i][1];
}

This is just returning an empty array. top10 is a multidimensional array which contains:

It can contain more data that's why I need a for loop. I am trying to create 2 multidimensional arrays "test1" and "test2" one will contain "Hinckley Train Station" and "4754" the other will contain "Hinckley Train Station" and "2274".

I can have multiple venues not just "Hinckley Train Station" "4754" "2274" I could also have "London City" "5000" "1000". This is why it is a for loop.

Hi I am trying to add some values to a multidimensional array using a for loop. This is what I have created so far:

var test1 = [];
var test2 = [];
for (var i = 0; i < top10.length; i++)
{
    test1[i] = i;
    test1[i][0] = top10[i][0];
    test1[i][1] = top10[i][1];
}

This is just returning an empty array. top10 is a multidimensional array which contains:

It can contain more data that's why I need a for loop. I am trying to create 2 multidimensional arrays "test1" and "test2" one will contain "Hinckley Train Station" and "4754" the other will contain "Hinckley Train Station" and "2274".

I can have multiple venues not just "Hinckley Train Station" "4754" "2274" I could also have "London City" "5000" "1000". This is why it is a for loop.

Share Improve this question asked Aug 16, 2017 at 12:40 Luke RaynerLuke Rayner 4091 gold badge7 silver badges22 bronze badges 2
  • It would be useful to post a subset of the data in top10 so we can fully replicate your code. – George Commented Aug 16, 2017 at 12:42
  • 1 Returning? There's nothing returning anything there? – Ruan Mendes Commented Aug 16, 2017 at 12:46
Add a ment  | 

2 Answers 2

Reset to default 2

You could push a new array to the wanted parts

var top10 = [
        ["Hinckley Train Station", "4754", "2274"],
        ["London City", "5000", "1000"]
    ],
    test1 = [],
    test2 = [],
    i;

for (i = 0; i < top10.length; i++) {
    test1.push([top10[i][0], top10[i][1]]);
    test2.push([top10[i][0], top10[i][2]]);
}

console.log(test1);
console.log(test2);
.as-console-wrapper { max-height: 100% !important; top: 0; }

In this line

test1[i] = i;

You are assigning an integer to be the first element of the outer array. You don't have a 2d array, you have an array of integers

In the following lines:

test1[i][0] = top10[i][0];
test1[i][1] = top10[i][1];

You are assigning properties to an integer, which means they are being boxed but the boxed value is thrown away.

It's hard to tell what you are trying to do, but the following is probably closer. You need to create a new inner array each time through the loop.

for (var i = 0; i < top10.length; i++)
{
    test1[i] = [];
    test1[i][0] = top10[i][0];
    test1[i][1] = top10[i][1];
    // Maybe do something similar with test2
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745429596a4627332.html

相关推荐

  • JavaScript Add Values to Multidimensional Array - Stack Overflow

    Hi I am trying to add some values to a multidimensional array using a for loop. This is what I have cre

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信