javascript - Why array is not iterable in reduce - Stack Overflow

I want to split array by even and odd elements, this is my codeA.reduce((a,v,i)=> v % 2 == 0 ? [...a

I want to split array by even and odd elements, this is my code

A.reduce((a,v,i)=> v % 2 == 0 ? [...a[0],v] : [...a[1],v],[[],[]])

A is array of numbers. I don't understand why do I get an error

a[1] is not iterable?

Considering that this code is working ok:

let arr = [[],[]];
console.log([...arr[1], 4]);

I want to split array by even and odd elements, this is my code

A.reduce((a,v,i)=> v % 2 == 0 ? [...a[0],v] : [...a[1],v],[[],[]])

A is array of numbers. I don't understand why do I get an error

a[1] is not iterable?

Considering that this code is working ok:

let arr = [[],[]];
console.log([...arr[1], 4]);

Share Improve this question edited Apr 26, 2019 at 16:33 Kamil Kiełczewski 92.9k34 gold badges395 silver badges370 bronze badges asked Apr 26, 2019 at 15:04 ogbofjnrogbofjnr 2,0485 gold badges27 silver badges49 bronze badges 7
  • What does the A array look like? – Vasil Dininski Commented Apr 26, 2019 at 15:05
  • @VasilDininski, I added in the post – ogbofjnr Commented Apr 26, 2019 at 15:06
  • arr.reduce... works fine, with your reduce function and arr definition. Seems most likely you've defined A as a 1d array. – junvar Commented Apr 26, 2019 at 15:07
  • 1 Once you've been through one iteration, you've spread the original a[0] or a[1] into the result array, so the accumulator is no longer a 2D array. – Pointy Commented Apr 26, 2019 at 15:07
  • 1 On a side note, do you believe this one liner is readable? :) – Icepickle Commented Apr 26, 2019 at 15:09
 |  Show 2 more ments

4 Answers 4

Reset to default 3

You are only returning a single array in reduce(). You also need to return the second. In the first iteration the a is [[],[]]. But after the first it will bee only a single array.

let A = [1,2,3,4]
const res= A.reduce((a,v,i)=> v % 2 == 0 ? [a[0],[...a[1],v]] : [[...a[0],v],a[1]],[[],[]])
console.log(res)

You could use a trick here. As v % 2 will return 1 or 0 so you could push() to that and use , to return the original a without spread operator.

let A = [1,2,3,4]
const res= A.reduce((a,v,i)=> (a[v % 2].push(v),a),[[],[]])
console.log(res)

You could also just filter twice:

  const res = [A.filter(it => it % 2), A.filter(it => !(it % 2))];

You can use destructuring assignment to make this a little easier -

const data =
  [ 1, 2, 3, 4 ]

const result =
  data.reduce
    ( ([ odd, even ], v) =>
        Boolean (v & 1)
          ? [ [...odd, v], even ]
          : [ odd, [...even, v] ]
    , [ [], [] ]
    )
    
console.log(result)
// [ [ 1, 3 ], [ 2, 4 ] ]

You can make a generic function, partition -

const partition = (p, a = []) =>
  a.reduce
    ( ([ t, f ], v) =>
        p (v)
          ? [ [...t, v], f ]
          : [ t, [...f, v] ]
    , [ [], [] ]
    )


const evenOdds =
  partition (v => Boolean (v & 1), [ 1, 2, 3, 4 ])

const lessThan2 =
  partition (v => v < 2, [ 1, 2, 3, 4 ])
    
console.log(evenOdds)
// [ [ 1, 3 ], [ 2, 4 ] ]

console.log(lessThan2)
// [ [ 1 ], [ 2, 3, 4 ] ]

The problem with your solution is that in reduce function you return one array of many elements (not one array with two arrays). Try this instead (where B=[[],[]], time plexity n )

A.forEach(x=> B[x%2].push(x) )

let A=[1,2,3,4,5,6,7], B=[ [],[] ];
A.forEach(x=> B[x%2].push(x) );

console.log(B);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744956333a4603215.html

相关推荐

  • javascript - Why array is not iterable in reduce - Stack Overflow

    I want to split array by even and odd elements, this is my codeA.reduce((a,v,i)=> v % 2 == 0 ? [...a

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信