根據(jù)數(shù)組中的某個(gè)字段生成對應(yīng)字段值相同的數(shù)組,例:下面根據(jù)type這個(gè)字段來將type值一致的分到同一數(shù)組。。。
let arr = [
{regulationsId: 5172, title: "測試111標(biāo)題2 ", type: 610, state: 1, createdTime: 1530152467000},
{regulationsId: 5169, title: "測試111標(biāo)題", type: 610, state: 1, createdTime: 1530085573000},
{regulationsId: 5170, title: "測試123標(biāo)題", type: 609, state: 1, createdTime: 1530085687000},
{regulationsId: 5171, title: "測試1122標(biāo)題", type: 608, state: 1, createdTime: 1530085750000}
];
//獲取數(shù)組中有多少個(gè)type
let types = [];
arr.map((item, index) => {
if(types.indexOf(item.type) === -1){
types.push(item.type)
}
})
//一個(gè)包含多個(gè)list的結(jié)果對象
let obj = [];
// 根據(jù)type生成多個(gè)數(shù)組
types.map((typeItem, typeIndex) => {
arr.map((arrItem, arrIndex) => {
if(arrItem.type == typeItem){
obj[typeIndex] = obj[typeIndex] || [];
obj[typeIndex].push(arrItem)
}
})
})
如果此文對你有用請動動你的小手點(diǎn)個(gè)贊!謝謝!??!