0
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)]

How can I obtain the same answer in JS?

var data = [50, 52, 54, 56, 58];
var answer = ??
print (answer);
Eric Bal
  • 1,115
  • 3
  • 12
  • 16

1 Answers1

3

map's callback argument three arguments. The element from the array, the index of the element in the array, and the array itself:

data.slice(1).map(function(el, index) {
  return [data[index], el];
});
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293