javascript odd and even separation in an array of numbers

前端 未结 5 1651
一整个雨季
一整个雨季 2021-01-06 19:04

i wants to separate an array with two groups (odd and even) sequentially. but when i try this:

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-06 19:34

    The way you do it is so complicated. You can simply achieve that with array.prototype.filter and array.prototype.concat:

    var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    arr = arr.filter(e => e%2).concat(arr.filter(e => e%2 === 0));
    console.log(arr);

提交回复
热议问题