我有两个简单的JavaScript数组,想在jqPlot上使用,需要按照以下格式整理数据:
[[[x1, sin(x1)], [x2, sin(x2)], ...]]
我的数组是:
$array_1 = [ "Meong", "Aumix" ];
$array_2 = [ 3, 2 ];
如何将它与最终输出合并,如下所示:
$output = [[['Meong', 3], ['Aumix', 2]]];
我尝试使用标准的jQuery合并和组合,但不起作用。 请帮忙。
我有两个简单的JavaScript数组,想在jqPlot上使用,需要按照以下格式整理数据:
[[[x1, sin(x1)], [x2, sin(x2)], ...]]
我的数组是:
$array_1 = [ "Meong", "Aumix" ];
$array_2 = [ 3, 2 ];
$output = [[['Meong', 3], ['Aumix', 2]]];
我尝试使用标准的jQuery合并和组合,但不起作用。 请帮忙。
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]);
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]);
reduce
来创建arrays
和objects
。 - Mihai Alexandru-Ionut