js将已有数组重新分组(将数组每10项分成一组)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

js将已有数组重新分组(将数组每10项分成一组)

杰哥斯坦森   2020-02-26 我要评论

项目中碰到的一个小需求:分页请求数据,一次请求60条,需要将后台返回的数组每10条分成一组渲染一个表格(表格使用的是ant-design-vue的table)


实现逻辑: var chunk = 10; var len = res.data.content.length; var result = []; for (let i = 10; i < len; i += chunk) { result.push(res.data.content.slice(i, i + chunk)) // 每10项分成一组 }
result = result.map((item, index) => { //遍历,每组加上表头和data
  return {
    columns: [{
      title: '列号',
      dataIndex: 'index',
      align: 'center',
      width: 60
    },
    {
      title: '特征',
      dataIndex: 'field',
      align: 'center',
      width: 110
    },
    {
      title: '分类',
      dataIndex: 'type',
      align: 'center',
      width: 110
    }],
    data: item
  }
})
this.tableData = result
 
//表格渲染部分
<a-table
  v-for="(item,index) in configTableData"
  :key="index"
  :columns="item.columns"
  :dataSource="item.data"
  bordered
  :rowKey="record=>record.id"
 >
   <template slot="title">
     组{{index+1}}
   </template>
</a-table>

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们