700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > js 循环数组 数组对象中某属性的值一样时 组成新的数组对象

js 循环数组 数组对象中某属性的值一样时 组成新的数组对象

时间:2021-07-15 14:45:24

相关推荐

js 循环数组 数组对象中某属性的值一样时 组成新的数组对象

处理前的源数组

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");

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。