function findRepeats(number = []) {
//过滤重复项并按正序排序
var notRepeat = number.filter((item, index, self) => self.indexOf(item) === index).sort();
var result = new Array();
console.log('过滤重复项', notRepeat)
//所有重复元素添加进新数组内
notRepeat.forEach(item => {
var t = number.filter(x => x == item);
if (t.length > 1) { result.push({ x: t }); }
});
return result;
}
var num = [2, 3, 1, 0, 1, 5, 0, 5, 5, 0, 0];
var a = findRepeats(num);
console.log(a);
> 过滤重复项 (5)[0, 1, 2, 3, 5]
> [{"0":[0,0,0,0]},{"1":[1,1]},{"5":[5,5,5]}]
网友评论