I really do not know how to put the question, I hope some one would assist me. this is what I want to achieve anyway. I want to use the google visualization
to create charts.
我真的不知道如何提问,我希望有人会帮助我。无论如何,这是我想要实现的目标。我想使用谷歌可视化来创建图表。
This is what I want to achieve.
这就是我想要实现的目标。
var arrayNeeded = [['Population', 'Total Number Per Unit'],['Staff Count', 5686],['Student Count', 9890]];
var data = google.visualization.arrayToDataTable(array);
This is what I have:
这就是我所拥有的:
var arr1 = [5686, 9890];
var arr2 = [Staff Count, Student Count];
How do I put these two arrays to give me the array called "arrayNeeded". here's what I have done. I'm a learner at javascript so it's kinda messy.
我如何把这两个数组给我一个名为“arrayNeeded”的数组。这就是我所做的。我是javascript的学习者,所以它有点混乱。
var obj = JSON.parse(sessionStorage.getItem('custs'));
var result = [];
for (var i = 0; i < obj.lgaid.length; i++)
{
result[i] = "['"+obj.lgan[i]+"',"+ parseInt(obj.lgaid[i])+"]";
}
obj is an object with two arrays lgan and lgaid in it
obj是一个对象,其中有两个数组lgan和lgaid
The result gives me an array of strings like this ("['Student Count', 9890]") rather than an array of 2-column arrays
结果给了我一个像这样的字符串数组(“['Student Count',9890]”)而不是一个2列数组的数组
var result = "['Population', 'Total Number Per Unit']","['Staff Count', 5686]","['Student Count', 9890]";
Please, someone, help me out. Thanks.
请有人帮助我。谢谢。
4 个解决方案
#1
1
You may try something like this:
你可以尝试这样的事情:
var arr1 = [5686, 9890];
var arr2 = ['Staff Count', 'Student Count'];
var result = arr1.map(function(value, index) {
return [arr1[index], arr2[index]];
});
result.unshift(['Population', 'Total Number Per Unit']);
#2
1
just use this
只是用这个
var required = [['Population', 'Total Number Per Unit']];
for(var i = 0; i < arr2.length; ++i) {
required.push([arr1[i],arr2[i]]);
}
the result array is in the required array.
结果数组位于所需的数组中。
#3
0
Create a new array by mapping one the arrays using using Array#map. The 2nd parameter in the callback is the current index, and you can use it to get the item from the other array.
通过使用Array#map映射一个数组来创建一个新数组。回调中的第二个参数是当前索引,您可以使用它从另一个数组中获取该项。
var arr1 = [5686, 9890];
var arr2 = ['Staff Count', 'Student Count'];
var result = arr1.map((v, index) => [arr2[index], v]);
console.log(result);
#4
0
Here is the answer as you needed. Hope this helps
这是您需要的答案。希望这可以帮助
var titleArr = ['Population', 'Total Number Per Unit']
var arr1 = [5686, 9890];
var arr2 = ['Staff Count', 'Student Count'];
var arrayNeeded = [];
arrayNeeded.push(titleArr);
arr1.map(function(item, index){
var tempArr = [];
tempArr.push(arr2[index], arr1[index]);
arrayNeeded.push(tempArr);
});
console.log(arrayNeeded);
#1
1
You may try something like this:
你可以尝试这样的事情:
var arr1 = [5686, 9890];
var arr2 = ['Staff Count', 'Student Count'];
var result = arr1.map(function(value, index) {
return [arr1[index], arr2[index]];
});
result.unshift(['Population', 'Total Number Per Unit']);
#2
1
just use this
只是用这个
var required = [['Population', 'Total Number Per Unit']];
for(var i = 0; i < arr2.length; ++i) {
required.push([arr1[i],arr2[i]]);
}
the result array is in the required array.
结果数组位于所需的数组中。
#3
0
Create a new array by mapping one the arrays using using Array#map. The 2nd parameter in the callback is the current index, and you can use it to get the item from the other array.
通过使用Array#map映射一个数组来创建一个新数组。回调中的第二个参数是当前索引,您可以使用它从另一个数组中获取该项。
var arr1 = [5686, 9890];
var arr2 = ['Staff Count', 'Student Count'];
var result = arr1.map((v, index) => [arr2[index], v]);
console.log(result);
#4
0
Here is the answer as you needed. Hope this helps
这是您需要的答案。希望这可以帮助
var titleArr = ['Population', 'Total Number Per Unit']
var arr1 = [5686, 9890];
var arr2 = ['Staff Count', 'Student Count'];
var arrayNeeded = [];
arrayNeeded.push(titleArr);
arr1.map(function(item, index){
var tempArr = [];
tempArr.push(arr2[index], arr1[index]);
arrayNeeded.push(tempArr);
});
console.log(arrayNeeded);