我可以将map()的索引增加2吗?

问题描述 投票:1回答:2

我有一个将数组转换为对象的问题。

const data = [0, 1, 2, 3, 4, 5];
const arr = data.map((element, idx) => {
  return {
    f: element,
    s: arr[idx + 1],
  };
});

当然,arr[{f: 0, s: 1}, {f: 1, s: 2}, ...],但我想将map的指数增加2.结果将是这样的:

arr = [{ f: 0, s: 1}, { f: 2, s: 3 }, ...]

有没有办法使用像map这样的方法来制作结果?

javascript arrays functional-programming
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.