数据
this.tableData = [ {id: 1, name: '测试', number: 1, price: 0}, {id: 2, name: '测试', number: 1, price: 0}, {id: 3, name: '测试', number: 1, price: 0}, {id: 4, name: '测试一', number: 1, price: 780}, {id: 5, name: '测试一', number: 1, price: 80}, {id: 6, name: '测试一', number: 1, price: 120}, {id: 7, name: '测试二', number: 1, price: 0} ]
函数
groupBy (array, name) { const groups = {} array.forEach(function (o) { const group = JSON.stringify(o[name]) groups[group] = groups[group] || [] groups[group].push(o) }) return Object.keys(groups).map(function (group) { return groups[group] }) }
调用
const sorted = this.groupBy(this.tableData, 'name') console.info('tag', sorted)
结果