在js中压缩数据数组

时间:2015-02-22 13:53:47

标签: javascript python

in python:
data = [50, 52, 54, 56, 58]
answer = list(zip(data[0:],data[1:]))
print answer
[(50, 52), (52, 54), (54, 56), (56, 58)]

如何在JS中获得相同的答案?

var data = [50, 52, 54, 56, 58];
var answer = ??
print (answer);

1 个答案:

答案 0 :(得分:3)

map's callback参数三个参数。数组中的元素,数组中元素的索引以及数组本身:

data.slice(1).map(function(el, index) {
  return [data[index], el];
});