js一行代码解实现数组去重和排序

//数组去重

方法一:
function dedupe(array){
    return Array.from(new Set(array));
}
console.log(dedupe([4,6,0,1,1,2,4,0,3]));//[4, 6, 0, 1, 2, 3]

方法二:
function dedupe(array){
    return [...new Set(array)];
}
console.log(dedupe([4,6,0,1,1,2,4,0,3]));//[4, 6, 0, 1, 2, 3]

//数组排序

function Sort(array){
    return array.sort(function(a, b){
       return a - b;//正序
    });
}
console.log(Sort([3,7,12,2,22,10,43,32,1]));//[1, 2, 3, 7, 10, 12, 22, 32, 43]

转载于:https://www.cnblogs.com/luowenshuai/p/9286435.html

你可能感兴趣的:(js一行代码解实现数组去重和排序)