Array.map want to map a value to nothing

Viewed 879

I have the array: [1,2,3,4,5,6,7]

I want to achieve: [[1,2], [3,4], [5,6], [7]]

I'm thinking Array.map, but it doesn't seem to be able to map to nothing for an element?

I have (using Underscorejs):

arr.map(function(el, idx, arr) {
  if (idx%2 != 0) return null;
  if (idx == arr.length-1) return [el];
  return [el, arr[idx+1]]
}).compact();

This is still a bit ugly. How can I achieve the transformation (without explicit loop)?

6 Answers
Related