gpt4 book ai didi

javascript - 动态构建二维数组Javascript

转载 作者:行者123 更新时间:2023-12-02 16:23:31 27 4
gpt4 key购买 nike

我想动态构建一个二维数组,但遇到了麻烦。

答案和投票(单字符串数组)的长度始终相同;

我需要一个像这样的数组:

var data = 
[['Answer', 'Votes'],
[answers[0], parseInt(votes[0])],
[answers[1], parseInt(votes[1])],
[answers[2], parseInt(votes[2])],
....
];

这不起作用;

var data[];
var arrayLength = answers.length;
for (var i = 0; i < arrayLength; i++) {
data += [answers[i], votes[i]]; //**Maybe the only line that needs tweaking?
}

最佳答案

试试这个:

var data = [];

data[0] = ['answers','votes']

var arrayLength = answers.length;
for (var i = 0; i < arrayLength; i++) {
data[i+1]= [answers[i], parseInt(votes[i])];
}

//check
console.table(data)

关于javascript - 动态构建二维数组Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28893121/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com