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);
答案 0 :(得分:3)
map
's callback
参数三个参数。数组中的元素,数组中元素的索引以及数组本身:
data.slice(1).map(function(el, index) {
return [data[index], el];
});