I have an array Arr1 = [1,1,2,2,3,8,4,6]
.
How can I split it into two arrays based on the odd/even-ness of element positions?
subArr1 = [1,2,3,4]
subArr2 = [1,2,8,6]
I have an array Arr1 = [1,1,2,2,3,8,4,6]
.
How can I split it into two arrays based on the odd/even-ness of element positions?
subArr1 = [1,2,3,4]
subArr2 = [1,2,8,6]
Or in more idiomatic CoffeeScript:
As a one-liner improvement to tokland's solution using underscore chaining function:
A functional approach using underscore:
[edit] Now there is _.partition:
I guess you can make 2 for loops that increment by 2 and in the first loop start with 0 and in the second loop start with 1
It would be easier using nested arrays:
if you're targeting modern browsers, you can replace the loop with
forEach
: