let array1 = [
{
"month": "03",
"year": "2024",
},
{
"month": "02",
"year": "2024",
},
{
"month": "12",
"year": "2023",
},
{
"month": "11",
"year": "2023",
},
{
"month": "11",
"year": "2023",
}
]
const findMax = (list) => {
list.forEach((element) => {
let yearArr = list.filter((item) => item.year === element.year)
if(yearArr.every((items) => element.month >= items.month)){
element.isMax = 1
}
});
return list;
}
console.log(findMax(array1))
//输出结果
[
{ month: '03', year: '2024', isMax: 1 },
{ month: '02', year: '2024' },
{ month: '12', year: '2023', isMax: 1 },
{ month: '11', year: '2023' },
{ month: '11', year: '2023' }
]
数组按照不同年份筛选最大月份
最新推荐文章于 2024-08-25 09:57:22 发布