Search code examples
javascriptjqueryarraysjqplot

Combine this array with jQuery, confused


I have two simple array in JavaScript, i want use it on jqPlot and required format data like this:

[[[x1, sin(x1)], [x2, sin(x2)], ...]]

My array is:

$array_1 = [ "Meong", "Aumix" ];
$array_2 = [ 3, 2 ];

How to combine/merge it with final output like as shown below:

$output = [[['Meong', 3], ['Aumix', 2]]];

I try use standar jQuery merge and combine not working. Please help.


Solution

  • You can use Array#map (or jQuery.map()) to iterate one of the arrays, and get the value from the 2nd array using the index:

    var $array_1 = [ "Meong", "Aumix" ];
    var $array_2 = [ 3, 2 ];
    
    var result = $array_1.map(function(item, index) {
      return [item, $array_2[index]];
    });
    
    console.log([result]);