最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript Add Values to Multidimensional Array - Stack Overflow

programmeradmin2浏览0评论

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
}
发布评论

评论列表(0)

  1. 暂无评论