js將已有數組重新分組(將數組每10項分成一組)


項目中碰到的一個小需求:分頁請求數據,一次請求60條,需要將后台返回的數組每10條分成一組渲染一個表格


實現邏輯:
var chunk = 10; var len = res.data.content.length; var result = []; for (let i = 0; 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 tableData"
  :key="index"
  :columns="item.columns"
  :dataSource="item.data"
  bordered
  :rowKey="record=>record.id"
 >
   <template slot="title">
     組{{index+1}}
   </template>
</a-table>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM