处理前的源数组
let applyList= [
{ applyNo: “1”, applyDate: “-04-22 10:41:31”, yearMonth: “04月” },
{ applyNo: “2”, applyDate: “-04-13 11:23:18”, yearMonth: “04月”},
{ applyNo: “3”, applyDate: “-03-04 09:37:49”, yearMonth: “03月” },
{ applyNo: “4”, applyDate: “-03-17 17:52:27”, yearMonth: “03月” },
];
处理后的目标数组
let applyListHandle= [
{yearMonth: “04月”,handle: [{ applyNo: “1”, applyDate: “-04-22 10:41:31”, yearMonth: “04月” },{ applyNo: “2”, applyDate: “-04-13 11:23:18”, yearMonth: “04月” }]},
{yearMonth: “03月”,handle: [{ applyNo: “3”, applyDate: “-03-04 09:37:49”, yearMonth: “03月” },{ applyNo: “4”, applyDate: “-03-17 17:52:27”, yearMonth: “03月” }]},
]
代码如下:
let tempArr = [];this.applyList.forEach((item, index) => {if (tempArr.indexOf(this.applyList[index].yearMonth) == -1) {this.applyListHandle.push({yearMonth: this.applyList[index].yearMonth,handle: [this.applyList[index]],});tempArr.push(this.applyList[index].yearMonth); } else {this.applyListHandle.forEach((it, i) => {if (this.applyListHandle[i].yearMonth ==this.applyList[index].yearMonth) {this.applyListHandle[i].handle.push(this.applyList[index]);}});}});// console.log(this.applyListHandle, "applyListHandle");