将形如 list 的数组转为 newList 多维数组
const list = [{
a1:11,
b2:12,
c3:13,
d4:14
},{
a1:21,
b2:22,
c3:23,
d4:24
},{
a1:31,
b2:32,
c3:33,
d4:34
}]
// 目标
const newList = [
[11,21,31],
[12,22,32],
[13,23,33],
[14,24,34]
]
解决方法:
// 方法一
const newList = {}
for(let i = 0;i<Object.values(list[0]).length;i++){
newList[i] = new Array
}
list.forEach((item,index) => {
for(let key in item){
newList[key].push(item[key])
}
})
array = Object.values(newList)
//输出
/*[
[11,21,31],
[12,22,32],
[13,23,33],
[14,24,34]
]*/
// 方法二【推荐】
const newList = []
for(let key in list[0]){
newList[key] = list.map(_ => _[key])
}
//输出,具有字符串索引
// [
// 'a1':[11,21,31],
// 'b2':[12,22,32],
// 'c3':[13,23,33],
// 'd4':[14,24,34]
// ]