1.获取数组中最大值
 var arr=[1,2,3,4,3,2,1];

   function getMax(arr){

        for(var i=0,max=arr[0];

            i<arr.length;

            arr[i]>max&&(max=arr[i]),i++);

        return max;//返回max

      }

      console.log(getMax(arr));//4
2.数组去重
方法一:

var arr=[“a”,”b”,”a”,”c”,”a”,”b”];

      function unique1(arr){

        //遍历arr中每个元素,同时声明空数组uarr

        for(var i=0,uarr=[];i<arr.length;i++){

          //遍历uarr中每个元素

          for(var j=0;j<uarr.length;j++){

           //如果uarr中当前元素等于arr中当前元素

            if(uarr[j]==arr[i]){

              break;//就退出循环

            }

          }//(遍历结束)

          //如果j等于uarrlength

            //arr中当前元素加入uarr

          j==uarr.length&&(uarr[j]=arr[i]);

        }//(遍历结束)

        return uarr;//返回uarr

      }  
方法二:
function unique2(arr){

        //遍历arr中每个元素,同时创建空数组hash

        for(var i=0,hash=[];i<arr.length;i++){

          //如果hash中以arr中当前元素值为key的元素等于undefined

          if(hash[arr[i]]===undefined){

            //就在hash中添加一个新元素,keyarr中当前元素,值为1

            hash[arr[i]]=1;

          }

        }//(遍历结束)hash:[“a”:1,”b”:1,”c”:1]

        i=0;//设置i=0

        var keys=[]//定义空数组keys

        for(keys[i++] in hash);

        return keys;

      }
3.冒泡排序
function bubbleSort(arr){

        //r1开始,到<length结束

        for(var r=1;r<arr.length;r++){

         

          for(var i=0;i<arr.length-r;i++){

                        if(arr[i]>arr[i+1]){

              //交换当前元素和下一个元素的位置

              //方法1: a+=b; b=a-b; a-=b;

              //方法2: b=[a,a=b][0];

              arr[i+1]=

                [arr[i],arr[i]=arr[i+1]][0];

            }

          }

        }

      }
4.插入排序
 function insertSort(arr){

        for(var i=1;i<arr.length;i++){

          var temp=arr[i];

          var p=i-1;

          while(temp<arr[p]&&p>=0){

            arr[p+1]=arr[p];

            p–;

          }

          arr[p+1]=temp;

        }

      }

5.快速排序
 function quickSort(arr){

        if(arr.length<=1){return arr;}

        else{

          var c=Math.floor(arr.length/2);

          var center=arr.splice(c,1)[0];

          var left=[],right=[];

          for(var i=0;i<arr.length;i++){

            if(arr[i]<=center){left.push(arr[i])}

            else{right.push(arr[i])}

          }

          return quickSort(left)

                  .concat(center,quickSort(right));

        }

      }

 

 

 

6.十进制转二进制
function dec2bin(dec){

        var bin=[];//声明空数组bin

        //只要dec>0,就反复

        while(dec>0){

          bin.unshift(dec%2);

                   dec=parseInt(dec/2);

        }//(遍历结束)

        return bin.join(“”);

      }
7.将英文句子中的每个单词首字母转大写
var str=”we two who and who”;

      //\b[a-z]+\b

      str=str.replace(/\b[a-z]/g,

        function(kwd){

          return kwd.toUpperCase();

        }

      );
8.每个单词后面添加逐渐增加的数字
var str=”we two who and who”;

      var i=5;

      str=str.replace(/\b[a-z]+\b/g,

        function(kwd){

          return kwd+i++;

        }

      );
9.删除开头或结尾空字符串
 function ltrim(str){

        return str.replace(/^\s+/,””);

      }

      function rtrim(str){

        //” \t张 东 \t” =>” \t张 东

        return str.replace(/\s+$/,””);

      }

      function trim(str){

        //” \t张 东 \t” =>”张 东

        return str.replace(/^\s+|\s+$/g,””);

      }
10.格式化
 var date=”20160830二下0434″;

      date=date.replace(

        /(\d{4})(\d{2})(\d{2})([\u4e00-\u9fa5])([\u4e00-\u9fa5])(\d{2})(\d{2})/,

        “$1$2$3日 星期$4 $5$6:$7″);

 

11.定义indexOf函数,可以从指定位置开始,查找下一个指定值得下标位置,如果找不到,返回-1

 

 

function indexOf(arr,val,fromi){

               fromi===undefined&&(fromi=0);

        for(;fromi<arr.length;fromi++){

                   if(arr[fromi]==val){

            return fromi;//返回fromi

          }

        }

        return -1; //返回-1

      }
12.扩展Array类型的indexOf方法
if(Array.prototype.indexOf===undefined){

        Array.prototype.indexOf=

          function(val,fromi){

                        fromi===undefined&&(fromi=0);

                        for(;fromi<this.length;fromi++){

                           if(this[fromi]==val){

                return fromi;              }

            }

            return -1;

          }

      }

13.判断一个对象是不是数组
var  obj={},arr=[],obj2={};obj2.__proto__=arr;
方法一:document.write(//isPrototypeOf

      “<br>”+Array.prototype.isPrototypeOf(obj)+

      “<br>”+Array.prototype.isPrototypeOf(arr)+

      “<br>”+Array.prototype.isPrototypeOf(obj2)

      );

方法二: document.write(//instanceof

      “<br>”+(obj instanceof Array)+

      “<br>”+(arr instanceof Array)+

      “<br>”+(obj2 instanceof Array)

      );

    

   

  方法三:    document.write(//Object.prototype.toString

        “<br>”+(Object.prototype.toString.call(obj)

          ==”[object Array]”)+

        “<br>”+(Object.prototype.toString.call(arr)

          ==”[object Array]”)+

    “<br>”+(Object.prototype.toString.call(obj2)

          ==”[object Array]”)

      );

   

    方法四:  //如果Array构造函数没有isArray

      if(Array.isArray===undefined){

        //Array的构造函数中添加isArray=

        Array.isArray=function(obj){

          //强行用obj调用Object.prototype.toString,如果结果是“[object Array]”,就返回true

          //否则,返回false

          return Object.prototype.toString

                       .call(obj)

                       ==”[object Array]”?

                                    true:false;

        }

      }

      document.write(//Array.isArray(obj);//ES5

        “<br>”+Array.isArray(obj)+

        “<br>”+Array.isArray(arr)+

        “<br>”+Array.isArray(obj2)

      );
14.实现一个js类型,既包含公有属性,又包含私有属性
 function Emp(id,name,age,salary){

        //public

        this.id=id;

        this.name=name;

        this.salary=salary;

        var _age;//private

        //配置属性的特性:

        Object.defineProperties(this,{

          id:{

            writable:false,configurable:false

          },

          salary:{//设置salary不可遍历

            enumerable:false,configurable:false

          },

          age:{//添加访问器属性保护局部变量_age

            set:function(val){

              if(val<18||val>65)

                throw new Error(

                  “年龄必须介于18~65之间“);

              _age=val;

            },

            get:function(){return _age},

            enumerable:true,

            configurable:true

          }

        });//this(当前对象)已经有了访问器属性age

        this.age=age;

        Object.seal(this);

      }
15.手写bind方法
if(Function.prototype.bind===undefined){

        //就在函数类型的原型中添加bind方法,定义1个参数obj

        Function.prototype.bind=

          function(obj/*,args1*/){

            var fun=this;//this->原函数对象

            var args1= Array.prototype.slice.call(

                arguments,1

              );//arguments.slice(1)`

            return function(/*args2*/){//返回新函数:

              var args2=

                Array.prototype.slice.call(

                  arguments

                );//arguments.slice();

                fun.apply(

                obj,args1.concat(args2)

              );

            }

          }

      }
16.手写foreach,map 方法
if(Array.prototype.forEach===undefined){

        //就为Array的原型添加forEach方法,参数定义为callback

        Array.prototype.forEach=

          function(callback){

            //遍历当前数组中每个元素

            for(var i=0;i<this.length;i++){

              //如果当前元素不是undefined

              if(this[i]!==undefined){

                //调用callback,传入当前元素值,当前位置,当前数组作为参数

                callback(this[i],i,this);

              }

            }

          }

      }
if(Array.prototype.map===undefined){

        //就为Array的原型添加map方法,参数定义为callback

        Array.prototype.map=

          function(callback){

            //创建一个空数组arr

            var arr=[];

            //遍历原数组中每个元素

            for(var i=0;i<this.length;i++){

              //如果原数组中当前元素不是undefined

              if(this[i]!==undefined){

                //调用callback,传入当前元素值,当前位置,原数组,将结果保存到arr的相同位置

                arr[i]=

                  callback(this[i],i,this);

              }

            }//(遍历结束)

            return arr;//返回arr

          }

      }

版权声明:本文为qinmanyu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/qinmanyu/p/10105289.html