js实现一个需求可能有上百种解法, 有的大费周章, 有的寥寥几笔, 如何让自己的代码又少又效率更高呢?
"234dsf".split("").reverse().join("") //"fsd432"
String.prototype.trim = function(){
return this.replace(/^\s+|\s+$/g, "")
}
3+" sdfsf ".trim()+5 //"3sdfsf5"
3+" sdfsf "+5 //3 sdfsf 5
[1,6,3,4,7].sort((a,b)=>b-a) //[7, 6, 4, 3, 1]
[1,6,3,4,7].sort((a,b)=>a-b) //[1, 3, 4, 6, 7]
[1,2,3,4,5,6].sort(function(){return Math.random()-0.5})
let arr=[
{sid:4,score:80},
{sid:1,score:39},
{sid:2,score:60},
{sid:3,score:60},
]
arr.sort((a,b)=>a.score==b.score?a.sid-b.sid:b.score-a.score)
Array.prototype.bubbleSort=function(){
if(this.length<=1){
return this
}
for(let i=0,len=this.length;i<len;i++){
for(let j=0,lenJ=this.length-i;j<lenJ;j++){
if(this[j]>this[j+1]){
this[j]=[this[j+1],this[j+1]=this[j]][0]
//交换两数的另一种写法
// this[j]=this[j] ^ this[j+1]
// this[j+1]=this[j] ^ this[j+1]
// this[j]=this[j] ^ this[j+1]
}
}
}
return this
}
[5,1,4,2,3].bubbleSort() //[1,2,3,4,5]
Array.prototype.insertSort=function(){
if(this.length<=1){
return this
}
for(let i=1,len=this.length;i<len;i++){
let temp=this[i],
p=i-1;
while(temp<this[p]&&p>=0){
this[p+1]=this[p];
p--;
}
this[p+1]=temp;
}
return this;
}
[5,1,4,2,3].insertSort() //[1,2,3,4,5]
Array.prototype.quickSort=function(){
if(this.length<=1){
return this
}else{
let c=Math.floor(this.length/2),
center=this.splice(c,1)[0],
left=[],right=[];
for(let i=0,len=this.length;i<len;i++){
if(this[i]<=center){
left.push(this[i])
}else{
right.push(this[i])
}
}
return [...arguments.callee.call(left),center,...arguments.callee.call(right)];
}
}
[5, 1, 4, 2, 3].quickSort() //[ 1, 2, 3, 4, 5 ]
Array.prototype.mergeSort=function(){
return function(low,high){
if(low>high){
return []
} else if(low==high){
return [this[low]]
}
let mid=Math.floor((low+high)/2),
res=[],
left=arguments.callee.call(this,low,mid),
right=arguments.callee.call(this,mid+1,high);
while(left.length>0 || right.length>0){
if(left[0]<right[0]){
res[res.length]=left.shift()
} else {
res[res.length]=right.shift()
}
if(left.length==0){
res=res.concat(right);
break
}else if(right.length==0){
res=res.concat(left);
break
}
}
return res
}.call(this,0,this.length-1)
}
[-11,17,12,19,0,155].mergeSort() //[ -11, 0, 12, 17, 19, 155 ]
Array.prototype.binarySearch=function(x){
var low=0,high=this.length-1;
while(low<=high){
var mid=Math.floor((low+high)/2);
if(x==this[mid]){
return mid;
}else if(x<this[mid]){
high=mid-1;
}else{
low=mid+1;
}
}
return -1;
}
[1, 5, 7, 3, 6, 9].binarySearch(7) //2
Array.prototype.unique = function() {
return this.filter(function(item, idx) {
return this.indexOf(item) === idx
})
}
let arr=[1,3,2,1,5,3,6,2,1,5,6,2]
arr=[...new Set(arr)]
String(arr).split(',')
function arrayDownDimension(arr){
let res=[];
for(let item of arr){
if(typeof item==='Array'){
res=res.concat(arrayDownDimension(item))
}else{
res[res.length]=item
}
}
return res
}
arrayDownDimension([[1,2,3,[9,8,[8,2,3,{a:3}]]],[4,5,6]])
//[ 1, 2, 3, 9, 8, 8, 2, 3, { a: 3 }, 4, 5, 6 ]
Array.prototype.max=function(){
return Math.max(...this)
}
[3, 5, 7, 4].max() //7
Array.prototype.min=function(){
return Math.min(...this)
}
[3, 5, 7, 4].min() //3
function isNumber(n){
return !isNaN(parseFloat(n)) && isFinite(n);
}
let a=(new Date()).getTime()
let b=+new Date()
//(0.0<=x<1.0)
Math.random()
//0<=r<=max
Math.floor(Math.random()*(max+1))
//min<=r<=max
Math.floor(Math.random()*(max+1-min))+min
a|0
a+.5|0
JSON.parse(JSON.stringify(arr))
//缺点:非json格式不能克隆,无法克隆原型对象
function urlQuery(url,name){
if(!url){
throw ("url不能为空")
}
if(!name){
throw ("name不能为空")
}
if(!url.includes('?')){
return
}
let arr=url.split('?')[1].split('&'),
str=name+'=';
for(let item of arr){
if(item.startsWith(str)){
return item.slice(str.length)
}
}
return ""
}
urlQuery("http://mail.163.com/?a=14&b=2&c=3&d=xxx&e","a") //14
function maxFactor(a,b){
if(a==b){
return a
}
let max,min;
if(a>b){
max=a;min=b
}else{
max=b;min=a
}
if(max%min==0){
return min
}
for(let i=min;i>1;i--){
if(max%i==0&&min%i==0){
return i;
}
}
return 1
}
let isPrime=(function(){
let hash={};
return function(n){
if(n<=3){
return true;
}else if(hash[n]!==undefined){
return hash[n];
}else{
for(let i=2,len=Math.sqrt(n);i<=len;i++){
if(n%i==0){
hash[n]=false;
return false;
}
}
hash[n]=true;
return true;
}
}
})();
(ps:如有错误,欢迎留言指正!如有问题,欢迎交流!)