请教一个js数据格式处理的问题,动态的属性名称

先上一个二维数据的格式格式

V: [
[
  "1",
  "2020-02-17",
  "2",
  "3",
  "4",
  "5",
  '6'
],
[
  "1",
  "2020-02-18",
  "2",
  "3",
  "4",
  "5",
  '6'
],
[
  "1",
  "2020-02-19",
  "2",
  "3",
  "4",
  "5",
  '6'
],
  ]
经过for循环处理后的二维数组
    for (let i = 0; i < this.list.V.length; i++) {
              for (let j = 0; j < this.list.V[i].length; j++) {
                this.excel.push({
                    // `num0${j}` :this.list.V[i][j]
                })
                console.log(`num0${j}`,this.list.V[i][j])
              }
            }

打印出来的样子
image.png

因为二维数组中的长度是随机的,下面是我想处理成这个样子的格式,和console出来的效果类似,想请教一下大佬应该怎么改,非常感谢

[
    {
        num00 : '1',
        num01 : '2020-02-17',
        num02 : '2',
        num03 : '3',
        num04 : '4',
        num05 : '5',
        num06 : '6',
    },
    {
        num00 : '1',
        num01 : '2020-02-18',
        num02 : '2',
        num03 : '3',
        num04 : '4',
        num05 : '5',
        num06 : '6',
    },
]


阅读 1.9k
4 个回答
let excel = v.map(arr => {
    let obj = {}
    arr.forEach((item,index)=>{
        obj['num' + ('0' + index).slice(-2)] = item
    })
    return obj
})
let result = dataList.map(itemArray => {
    return itemArray.reduce((acc, curr, index) => {
        let key = 'num' + String(index).padStart(2, '0')
        return {
            ...acc,
            [key]: curr,
        }
    }, {})
})

console.log('result: ', result)

const v = [
  ["1", "2020-02-17", "2", "3", "4", "5", "6"],
  ["1", "2020-02-18", "2", "3", "4", "5", "6"],
  ["1", "2020-02-19", "2", "3", "4", "5", "6"]
];
v.map(
  i=>i.reduce(
    (obj, j, jNo)=>(obj[`num${('0' + jNo).substr(-2)}`] = j) && obj,
    {}
  )
)
 arr.map(item => {
  let res = {}
  if(Array.isArray(item) && item.length > 0) {
    item.forEach((it, index) => {
       res['num0' + index] = it
    })
  }
  return res
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题