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]
You could try:
JS Fiddle demo.
Just for fun, in two lines, given that it's been tagged coffeescript :
A method without modulo operator:
filters
is a non-static & non-built-in Array method , which acceptsliteral object
of filters functions & returns aliteral object
of arrays where the input & the output are mapped by object keys.