我必须在传单地图上为标记准备数据,我的输入是:
popup = ["abc", "bza", "ghh"];
myCol1 = [16.51545, 16.15454, 16.1454];
myCol2 = [80.2515, 80.1545, 80.5645];
我需要的是:
myData = [
["abc", 16.51545, 80.2515],
["bza", 16.15454, 80.1545],
["ghh", 16.1454, 80.5645]
];
答案 0 :(得分:4)
您可以将Array#map
与arrow function一起使用。
popup.map((e, i) => [e, myCol1[i], myCol2[i]])
等同于ES5:
popup.map(function(element, index) {
return [element, myCol1[index], myCol2[index]];
});
var popup = ["abc", "bza", "ghh"],
myCol1 = [16.51545, 16.15454, 16.1454],
myCol2 = [80.2515, 80.1545, 80.5645];
var data = popup.map((e, i) => [e, myCol1[i], myCol2[i]]);
console.log(data);
document.body.innerHTML = '<pre>' + JSON.stringify(data, 0, 4) + '</pre>';
&#13;
答案 1 :(得分:0)
只要数组的长度始终与彼此相同,一个简单的for循环就足够了:
var myData = [];
for(var i = 0; i < popup.length; i++) {
myData.push([popup[i], myCol1[i], myCol2[i]]);
}
答案 2 :(得分:0)
使用Array#reduce()
和
Array#forEach()
嵌套:
var popup = ["abc", "bza", "ghh"],
myCol1 = [16.51545, 16.15454, 16.1454],
myCol2 = [80.2515, 80.1545, 80.5645],
myData = [popup, myCol1, myCol2].reduce(function (r, a, i) {
a.forEach(function (b, j) {
r[j] = r[j] || [];
r[j][i] = b;
});
return r;
}, []);
document.write('<pre>' + JSON.stringify(myData, 0, 4) + '</pre>');