简体   繁体   English

基于奇数/偶数位置将数组拆分为两个数组

[英]split an array into two arrays based on odd/even position

I have an array Arr1 = [1,1,2,2,3,8,4,6] . 我有一个数组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]
odd  = arr.filter (v) -> v % 2
even = arr.filter (v) -> !(v % 2)

Or in more idiomatic CoffeeScript: 或者更惯用的CoffeeScript:

odd  = (v for v in arr by 2)
even = (v for v in arr[1..] by 2)

You could try: 你可以尝试:

var Arr1 = [1,1,2,2,3,8,4,6],
    Arr2 = [],
    Arr3 = [];

for (var i=0;i<Arr1.length;i++){
    if ((i+2)%2==0) {
        Arr3.push(Arr1[i]);
    }
    else {
        Arr2.push(Arr1[i]);
    }
}

console.log(Arr2);

JS Fiddle demo . JS小提琴演示

It would be easier using nested arrays: 使用嵌套数组会更容易:

result = [ [], [] ]

for (var i = 0; i < yourArray.length; i++)
    result[i & 1].push(yourArray[i])

if you're targeting modern browsers, you can replace the loop with forEach : 如果您的目标是现代浏览器,则可以使用forEach替换循环:

yourArray.forEach(function(val, i) { 
    result[i & 1].push(val)
})

A functional approach using underscore : 使用下划线的功能方法:

xs = [1, 1, 2, 2, 3, 8, 4, 6]
partition = _(xs).groupBy((x, idx) -> idx % 2 == 0)
[xs1, xs2] = [partition[true], partition[false]]

[edit] Now there is _.partition : [编辑]现在有_.partition

[xs1, xs2] = _(xs).partition((x, idx) -> idx % 2 == 0)
var Arr1 = [1, 1, 2, 2, 3, 8, 4, 6]
var evenArr=[]; 
var oddArr = []

var i;
for (i = 0; i <= Arr1.length; i = i + 2) {
    if (Arr1[i] !== undefined) {
        evenArr.push(Arr1[i]);
        oddArr.push(Arr1[i + 1]);
    }
}
console.log(evenArr, oddArr)

As a one-liner improvement to tokland's solution using underscore chaining function: 作为使用下划线链接功能的tokland解决方案的单线改进:

xs = [1, 1, 2, 2, 3, 8, 4, 6]
_(xs).chain().groupBy((x, i) -> i % 2 == 0).values().value()

filters is a non-static & non-built-in Array method , which accepts literal object of filters functions & returns a literal object of arrays where the input & the output are mapped by object keys. filters是一个非静态&非内置在阵列的方法,它接受literal object的过滤器的功能&返回一个literal object ,其中输入和输出是通过对象键映射阵列。

  Array.prototype.filters = function (filters) { let results = {}; Object.keys(filters).forEach((key)=>{ results[key] = this.filter(filters[key]) }); return results; } //---- then : console.log( [12,2,11,7,92,14,5,5,3,0].filters({ odd: (e) => (e%2), even: (e) => !(e%2) }) ) 

我猜你可以为2增加2的循环,在第一个循环中以0开始,在第二个循环中以1开始

A method without modulo operator: 没有模运算符的方法:

var subArr1 = [];
var subArr2 = [];
var subArrayIndex = 0;
var i;
for (i = 1; i < Arr1.length; i = i+2){
    //for even index
    subArr1[subArrayIndex] = Arr1[i];
    //for odd index
    subArr2[subArrayIndex] = Arr1[i-1];
    subArrayIndex++;
}
//For the last remaining number if there was an odd length:
if((i-1) < Arr1.length){
    subArr2[subArrayIndex] = Arr1[i-1];
}

Just for fun, in two lines, given that it's been tagged coffeescript : 只是为了好玩,两行,因为它被标记为coffeescript:

Arr1 = [1,1,2,2,3,8,4,6]

[even, odd] = [a, b] = [[], []]
([b,a]=[a,b])[0].push v for v in Arr1

console.log even, odd
# [ 1, 2, 3, 4 ] [ 1, 2, 8, 6 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM