admin管理员组文章数量:1405904
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.
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
The result gives me an array of strings like this ("['Student Count', 9890]") rather than an array of 2-column arrays
var result = "['Population', 'Total Number Per Unit']","['Staff Count', 5686]","['Student Count', 9890]";
Please, someone, help me out. Thanks.
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.
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
The result gives me an array of strings like this ("['Student Count', 9890]") rather than an array of 2-column arrays
var result = "['Population', 'Total Number Per Unit']","['Staff Count', 5686]","['Student Count', 9890]";
Please, someone, help me out. Thanks.
Share Improve this question edited Sep 7, 2017 at 11:10 shishir 8493 gold badges11 silver badges27 bronze badges asked Sep 7, 2017 at 9:53 Israel EruogheneIsrael Eruoghene 972 silver badges14 bronze badges 2- 1 Have you looked at array.push(); or array.contact();? – Granny Commented Sep 7, 2017 at 9:55
- @Isreal: i have answered below on how you needed it. Also, now you can have any number of values in you arr1 and arr2. and it will join and give you the correct result. Hope it helps. – mechanicals Commented Sep 7, 2017 at 10:05
4 Answers
Reset to default 2You 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']);
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.
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.
var arr1 = [5686, 9890];
var arr2 = ['Staff Count', 'Student Count'];
var result = arr1.map((v, index) => [arr2[index], v]);
console.log(result);
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);
本文标签: javascripthow to create an array from two arraysStack Overflow
版权声明:本文标题:javascript - how to create an array from two arrays - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744905580a2631601.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论