原型冒泡与排重

119 阅读1分钟
  • 原型冒泡排序-小到大
var arr = [1, 5, 13, 17, 22, 15, 8, 6];
Array.prototype.aSort = function () {
   for (let i = 0; i < this.length; i++) {
    for (let j = 0; j < this.length; j++) {
        if (this[j - 1] > this[j]) {
            var str;
            str=this[j];
            this[j]=this[j-1];
            this[j-1]=str
           }
          }
         }
          return this;
       }
        console.log(arr.aSort())
  • 原型排重 push()以接收任意数量的参数,把它们逐个添加到数组末尾,并返回修改后数组的长度
  var arr = [5, 4, 26, 9, 4, 8, 5, 14];
    Array.prototype.delRepeat = function () {
     var str = [];
      for (let i = 0; i < this.length; i++) {
        if(str.indexOf(this[i])==-1){
            str.push(this[i])
            }
           }
           return sta
         }
      console.log(arr.delRepeat())