let arr = [1,2,3,1,2,3,2,1,3,4,4,5]
const nary = arr.reduce((total, item, index) => {
if (total[item]) total[item].push(index);
else total[item] = [index];
if (index === arr.length - 1)
Object.keys(total).map((key) => {
return total[key].length <= 1 && delete total[key];
});
return total;
}, {});
console.log(nary)
//打印出的结果
{
1:[0,3,7],
2:[1,4,6],
3:[2,5,8],
4:[9:10]
}
数组中找到相同的数据和其下标(有可能多组重复的数据)
最新推荐文章于 2024-01-19 09:23:43 发布