根据数组中的某个字段生成对应字段值相同的数组,例:下面根据type这个字段来将type值一致的分到同一数组。。。
let arr = [
{regulationsId: 5172, title: "测试111标题2 ", type: 610, state: 1, createdTime: 1530152467000},
{regulationsId: 5169, title: "测试111标题", type: 610, state: 1, createdTime: 1530085573000},
{regulationsId: 5170, title: "测试123标题", type: 609, state: 1, createdTime: 1530085687000},
{regulationsId: 5171, title: "测试1122标题", type: 608, state: 1, createdTime: 1530085750000}
];
//获取数组中有多少个type
let types = [];
arr.map((item, index) => {
if(types.indexOf(item.type) === -1){
types.push(item.type)
}
})
//一个包含多个list的结果对象
let obj = [];
// 根据type生成多个数组
types.map((typeItem, typeIndex) => {
arr.map((arrItem, arrIndex) => {
if(arrItem.type == typeItem){
obj[typeIndex] = obj[typeIndex] || [];
obj[typeIndex].push(arrItem)
}
})
})
如果此文对你有用请动动你的小手点个赞!谢谢!!!