如何操作数组

如何操作数组

var arr = [{1:1,2:2,3:3,4:4},{1:1,2:2,3:3,4:4},{1:1,2:2,3:3,4:4},{1:1,2:2,3:3,4:4}]

这种格式数组如何改成

[1,1,1,1],

[2,3,2,2],

[3,3,3,3],

[4,4,4,4],

第一个数组取第一个数组对象的值以此类推


回答:

var arr = [{1:1,2:2,3:3,4:4},{1:1,2:2,3:3,4:4},{1:1,2:2,3:3,4:4},{1:1,2:2,3:3,4:4}]

var l = arr.map(v => Object.values(v))

console.log(l)

------------------------好像看错题了-----------------------

  var arr = [{ 1: 1, 2: 2, 3: 3, 4: 4 }, { 1: 1, 2: 2, 3: 3, 4: 4 }, { 1: 1, 2: 2, 3: 3, 4: 4 }, { 1: 1, 2: 2, 3: 3, 4: 4 }]

var l = arr.map((v, i) => {

const f = []

for (let b = 0; b < arr.length; b++) {

f.push(Object.values(arr[b])[i])

}

return f

})

console.log(l)


回答:

let filter=arr.map(n=>Object.values(n))


回答:

var arr = [{1:1,2:2,3:3,4:4},{1:1,2:2,3:3,4:4},{1:1,2:2,3:3,4:4},{1:1,2:2,3:3,4:4}]

Object.values(arr.reduce((acc,cur)=>(Object.entries(cur).forEach(([key,val])=>((acc[key]||=[]).push(val))),acc),{}));

输出
0: (4) [1, 1, 1, 1]
1: (4) [2, 2, 2, 2]
2: (4) [3, 3, 3, 3]
3: (4) [4, 4, 4, 4]

以上是 如何操作数组 的全部内容, 来源链接: utcz.com/p/935621.html

回到顶部