将这个数组与jQuery组合起来,一头雾水

Combine this array with jQuery, confused

我在 JavaScript 中有两个简单的数组,我想在 jqPlot 上使用它,并且需要这样的格式数据:

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

我的数组是:

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

如何 combine/merge 最终输出如下所示:

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

我尝试使用标准 jQuery 合并和组合不起作用。 请帮忙。

您可以使用reduce方法。

$array_1 = [ "Meong", "Aumix" ];
$array_2 = [ 3, 2 ];
let finalArray=$array_1.reduce(function(acc,elem,i){
  acc.push([elem,$array_2[i]]);
  return acc;
},[]);
console.log([finalArray]);

您可以使用 Array#map (or jQuery.map()) 迭代其中一个数组,并使用 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]);