Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 699
Opsional Avatar asked May 17 '26 01:05

Opsional


1 Answers

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]);
like image 182
Ori Drori Avatar answered May 18 '26 14:05

Ori Drori