javascript - 将两个整数数组添加到数组数组中,以便创建坐标对

标签 javascript arrays

我有两个数组,一个是 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/

相关文章:

javascript - 在 HTML5 视频中突出显示播放器搜索栏

javascript - cookie中出现乱码

javascript - 如何仅使用 css 为 "simple jquery filtering"设置动画

C 程序问题 : scanf isn't working properly

c++ - 创建对象数组时出现段错误

javascript - 如何获取对象数组中最大值的索引?

javascript - 注册jQuery点击,第一次和第二次点击

javascript - 打开 js 时破坏功能规范 : true - Capybara - Rails

java - 如何更正我的方法以便从数组中删除字符串?

c# - 多维数组不实现 IEnumerable<T>,或者它们实现了吗?