javascript - how to create an array from two arrays - Stack Overflow

I really do not know how to put the question, I hope some one would assist me. this is what I want to a

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
Add a ment  | 

4 Answers 4

Reset to default 2

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']);

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);

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

相关推荐

  • javascript - how to create an array from two arrays - Stack Overflow

    I really do not know how to put the question, I hope some one would assist me. this is what I want to a

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信