【发布时间】:2021-04-25 21:11:45
【问题描述】:
我有数组
const example = [{ day: 5, month: 1, key: 1 },{ day: 5, month: 1, key: 2 },{ day: 3, month: 3, key: 3 },{ day: 5, month: 1, key: 4 },{ day: 3, month: 3, key: 5 },{ day: 11, month: 4, key: 6 }];
当日月元素相同时如何查看?如果年份和月份重复我希望结果返回
{err:'the same',index:[0,1,2,3]}
我的代码不起作用。
const duplicates = Object.entries(array).reduce((acc, item, index) => {
if (acc[1].year === item[1].year && acc[1].month === item[1].month) {
return [
{
index:[acc[0],acc[1]],
err: "the same"
}
];
}
return acc;});
【问题讨论】:
标签: javascript arrays algorithm object reduce