标签:
var arr = [1,5,2,56,12,34,21,3,5] Math.min.apply({},arr) // 1 Math.max.apply({},arr) // 56 arr.sort((m,n)=>m-n) // [1, 2, 3, 5, 5, 12, 21, 34, 56] arr.sort((m,n)=>n-m) //[56, 34, 21, 12, 5, 5, 3, 2, 1]
去重复
var arr = [2,1,4,3,2,4,2,3,4,2,6,5,5] var obj = {}; var arrNew = []; for(var i=arr.length-1;i>=0;i--){ if(!obj[arr[i]]){ obj[arr[i]] = true; arrNew.push(arr[i]); } } console.log(arrNew,obj) // [5, 6, 2, 4, 3, 1]
Array.prototype.unique2 = function(){ this.sort(); //先排序 var res = [this[0]]; for(var i = 1; i < this.length; i++){ if(this[i] !== res[res.length - 1]){ res.push(this[i]); } } return res; } var arr = [1, ‘a‘, ‘a‘, ‘b‘, ‘d‘, ‘e‘, ‘e‘, 1, 0] console.log(arr.unique2());
js数组操作 求最大值,最小值,正序、倒叙大小值排序,去重复
标签:
原文地址:http://www.cnblogs.com/liujinyu/p/5624279.html