js 将一个数组对象分成两组

  • Post author:
  • Post category:其他


let mapArr = [
    {
        name: 'aaa',
        state: 0,
        mark: 'au'
    },
    {
        name: 'bbb',
        state: 1,
        mark: 'auu'
    },
    {
        name: 'ccc',
        state: 2,
        mark: 'auuu'
    },
    {
        name: 'ddd',
        state: 3,
        mark: 'auuuu'
    }
]
let temp = [];
let ranm = [];
let zongArr = []
mapArr.forEach( (item, index)=> {
    if (index % 2) {
        temp.push(item)
    } else {
        ranm.push(item)
    }
})

ranm.forEach( (item, index)=> {
    zongArr.push([item,temp[index]])
})

zongArr就是得到的数组



版权声明:本文为sunlizhen原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。