我有两个数组,一个是 x 坐标,一个是 y 坐标。我需要创建一个新的数组数组来对坐标进行分组。
yCord = [30, 29, 31, 32];
xCord = [0, 1, 2 ,3];
这是我得到的。
var cordPairs = {};
xCord.forEach((key, i) => cordPairs[key] = yCord[i]);
但是这对对象中的值进行了配对,我现在需要一个数组。
{0: 30, 1: 29, 2: 31, 3: 32}
期望的结果:
cordPairs = [[0, 30], [1, 29], [2, 31], [3, 32]]
最佳答案
您需要 array
格式的数据,所以这应该可以工作:
const yCord = [30, 29, 31, 32];
const xCord = [0, 1, 2 ,3];
const coords = xCord.map((el, index)=> [el, yCord[index]]);
console.log(coords);
关于javascript - 将两个整数数组添加到数组数组中,以便创建坐标对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55400844/