欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

array原型方法实现(push pop unshift shift slice splice reverse sort reduce indexOf find findIndex)

程序员文章站 2023-12-23 14:00:04
...

array原型方法实现

push pop unshift shift slice splice reverse sort reduce reduceRight concat join every some filter map indexOf lastIndexOf find findIndex includes

let arr = [0, 1, 2, 3]
let len = arr.push(4); // 返回数组的个数, 改变原数组
// console.log(len) // 5
/**
 * push 实现改变原数组 
 */
const prototype = Array.prototype;
prototype.push1 = function () {
  let len = this.length;
  let args = arguments;
  for (var i = 0; i < args.length; i++) {
    this[len] = args[i];
    len++
  }
  return this.length;
}
// console.log(arr.push1(4)); // 6

let item = arr.pop(); // 返回数组的删除项,改变原数组
// console.log(item);

/**
 * pop
 */
prototype.pop1 = function () {
  let lastItem = this[this.length - 1];
  this.length = this.length - 1;
  return lastItem;
}
// console.log(arr.pop1()); // 3

let len1 = arr.unshift(-1, -2); // 返回数组长度,改变原数组
// console.log(arr, len1)

/**
 * unshift
 */
prototype.unshift1 = function () {
  let args = arguments;
  let len = this.length;
  // 把数组所有项向后移动arguments.length位
  for (var i = len - 1; i >= 0; i--) {
    this[i + args.length] = this[i]
  }
  for (var j = 0; j < args.length; j++) {
    this[j] = args[j]
  }
  return this.length;
}
// console.log(arr, arr.unshift1(-3, -4 ,-5));
let firstItem = arr.shift(); // 返回数组[0]的删除项,改变原数组
// console.log(arr, firstItem)

/**
 * shift
 */
prototype.shift1 = function () {
  let firstItem = this[0];
  let len = this.length;
  // 数组向前移动一位
  for (var i = 0; i < len; i++) {
    this[i] = this[i + 1]
  }
  this.length--;
  return firstItem;
}
// console.log(arr, arr.shift1());

// console.log(arr, arr.slice(1, 3)) // 返回一个新数组,原数组不变
// console.log(arr.slice(undefined, 2)) // 0~2
/**
 * slice
 */
prototype.slice1 = function () {
  let arr = []
  let start = arguments[0] || 0;
  if (start < 0) {
    start = this.length + start;
  }
  let end = arguments[1] || this.length;
  if (end > this.length) {
    end = this.length;
  }
  if (end < 0) {
    end = this.length + end;
  }
  if (start >= end) {
    return arr;
  }
  for (var i = 0; i < this.length; i++) {
    if (i >= start && i < end) {
      arr.push(this[i])
    }
  }
  return arr;
}
// console.log(arr, arr.slice1(1, 3));
// console.log(arr.slice1(0))

// console.log(arr.splice(0, 2, 11, 22)); // 返回删除项,改变原数组
/**
 * splice
 * 第一个参数开始位置
 * 第二个参数删除的个数
 * 第三个参数~第n个参数表示插入的项
 */
prototype.splice1 = function () {
  let start = arguments[0] || 0;
  let deleteNum = arguments[1] || 0;
  let len = this.length;
  // 插入的值
  let add = [];
  for (var i = 2; i < arguments.length; i++) {
    add.push(arguments[i]);
  }
  // 计算删除的结束位置
  let end = start + deleteNum > len ? len : start + deleteNum;
  let deleteItems = this.slice(start, end)
  let newArr = this.slice(0, start);
  let next = this.slice(start + end);
  add.forEach(item => newArr.push(item));
  next.forEach(item => newArr.push(item));
  // this = this.slice(0, start).concat(this.slice(start + end))
  for (var j = 0; j < newArr.length; j++) {
    this[j] = newArr[j]
  }
  return deleteItems;
}
console.log(arr, arr.splice1(0, 1, 33, 44, 55, 12))

// console.log(arr.reverse()); // 翻转数组,改变原数组
/**
 * reverse
 */
prototype.reverse1 = function () {
  let arr = this.slice(0)
  let j = 0;
  for (var i = arr.length - 1; i >= 0; i--) {
    this[j] = arr[i]
    j++
  }
  return this;
}
// console.log(arr.reverse1()); // 翻转数组,改变原数组

// console.log(arr.sort()); // [ 0, 1, 12, 2, 3, 33, 44, 55 ] 排序,改变原数组

/**
 * sort
 * 接收一个回调函数作为参数
 */
let arr1 = [{
  n: 2
}, {
  n: 1
}, {
  n: 11
}, {
  n: 22
}]
let arr2 = [{
  name: 'AAS'
}, {
  name: 'DSF'
}, {
  name: 'FSD'
}, {
  name: 'ABC'
}]
prototype.sort1 = function (callback) {
  for (let i = 0; i < this.length; i++) {
    for (let j = 0; j < this.length - 1 - i; j++) {
      let result;
      if (typeof callback === 'undefined') {
        result = (this[j]).toString() > (this[j + 1]).toString();
      } else if (typeof callback(this[j], this[j + 1]) === 'number') {
        result = callback(this[j], this[j + 1]) > 0;
      } else {
        result = callback(this[j], this[j + 1]);;
      }
      if (result) {
        let temp = this[j];
        this[j] = this[j + 1];
        this[j + 1] = temp;
      }
    }
  }
  return this;

}

// console.log(arr2.sort1(function (a, b) {
//   let val1 = a.name;
//   let val2 = b.name;
//   return val1 < val2;
// }))

/**
 * reduce
 */
prototype.reduce1 = function (callback, init) {
  for (var i = 0; i < this.length; i++) {
    if (init === undefined) {
      init = callback(this[i], this[i + 1], i + 1, this)
      i++;
    } else {
      init = callback(init, this[i], i, this)
    }
  }
  return init;
}
// console.log(arr.reduce1(function(a, b) {
//     return a + b;
// }))
/**
 * reduceRight
 * @param {*} callback 
 * @param {*} init 
 */
prototype.reduceRight1 = function(callback, init) {
  let len = this.length;
  for (var i = len - 1; i >= 0; i--) {
    if (init === undefined) {
      init = callback(this[i], this[i - 1], i - 1, this)
      i--;
    } else {
      init = callback(init, this[i], i, this)
    }
  }
  return init;
}
// let count = arr.reduceRight1((a, b, index) => {
//   console.log(a, index)
//   return a + b;
// })
// console.log(count);

let concat1 = arr.concat([4,6,9], 10, 12) // 返回新数组,不改变原数组
console.log(concat1)

prototype.concat1 = function() {
  let args = arguments;
  let newArr = []
  for (var j = 0; j < this.length; j++) {
    newArr.push(this[j])
    
  }
  for (var i = 0; i < args.length; i++) {
    if (args[i] instanceof Array) {
      for (var k = 0; k < args[i].length; k++) {
        newArr.push(args[i][k])
      }
    } else {
      newArr.push(arr[i]);
    }
  }
  return newArr;
}
// console.log(arr.join(1)) // 返回拼接后的字符串,不改变原数组
/**
 * join
 */
prototype.join1 = function(de) {
  de = de || ',';
  let str = '';
  for (let i = 0;i < this.length; i++) {
    str = str + de + this[i];
  }
  return str.slice(1);
}
// console.log(arr.join1(1));

// console.log(arr.every((item, i) => i > 10)); // 所有值都满足条件返回true, 否则返回false,并终止循环
/**
 * every
 * 接收一个函数作为参数
 */
prototype.every1 = function(callback) {
  let flag = true;
  for (let i = 0; i < this.length; i++) {
    if (!callback(this[i], i, this)) {
      flag = false;
      break;
    }
  }
  return flag;
}
// console.log(arr.every1((item, i) => item >= 0))

// console.log(arr.some(item => item > 10))
/**
 * 
 * @param {*} callback 
 * callback三个参数
 * item ,index ,数组本身
 */
prototype.some1 = function(callback) {
  let flag = false;
  for (let i = 0; i < this.length; i++) {
    if (callback(this[i], i, this)) {
      flag = true;
      break;
    }
  }
  return flag;
}
// console.log(arr.some1(item => item > 100))

// console.log(arr.filter(item => item > 2))

/**
 * 
 * @param {*} callback 
 * item, index, 数组本身
 */
prototype.filter1 = function(callback) {
  let arr = [];
  for (let i = 0; i < this.length; i++) {
    if (callback(this[i], i, this)) {
      arr.push(this[i])
    }
  }
  return arr;
}
// console.log(arr.filter1(item => item > 2))

let m1 = [{age: 1}, {age: 11}, {age: 22}];
// console.log(m1.map(item => item.age));

prototype.map1 = function(callback) {
  let arr = [];
  for (let i = 0; i < this.length; i++) {
    arr.push(callback(this[i], i, this))
  }
  return arr;
}
// console.log(m1.map1(item => item.age))

// console.log(arr.indexOf(2)) // 返回索引,未找到返回-1
/**
 * 
 * @param {*} item 查找项
 * @param {*} n 起始位置
 */
prototype.indexOf1 = function(item, n) {
  let index = -1;
  n = n || 0;
  for (let i = n; i < this.length; i++) {
    if (item === this[i]) {
      index = i
      break
    }
  }
  return index;
}
// console.log(arr.indexOf1(2, 7))

prototype.lastIndexOf1 = function(item, n) {
  let index = -1;
  n = n || this.length - 1;
  if (n >= this.length) {
    n = this.length - 1;
  }
  if (n < 0 && Math.abs(n) > this.length) {
    return index;
  }
  for (let i = n; i >= 0; i--) {
    if (item === this[i]) {
      index = i;
    }
  }
  return index;
}
console.log(arr.lastIndexOf1(2));
// console.log(m1.find(item => item.age === 11));
/**
 * 
 * @param {*} callback 回调函数
 */
prototype.find1 = function(callback) {
  let result;
  for (let i = 0; i < this.length; i++) {
    if (callback(this[i], i, this)) {
      result = this[i]
      break;
    }
  }
  return result;
}
// console.log(m1.find1(item => item.age === 223));

// console.log(m1.findIndex(item => item.age === 223)); // 接收函数作为参数返回索引
/**
 * 
 * @param {*} callback
 */
prototype.findIndex1 = function(callback) {
  let index = -1;
  for (let i = 0; i < this.length; i++) {
    if (callback(this[i], i, this)) {
      index = i;
      break;
    }
  }
  return index;
}

// console.log(m1.findIndex1(item => item.age === 11));

console.log(m1.includes(11));

prototype.includes1 = function(item, n) {
  let flag = false;
  n = (n || 0) < 0 ? n + this.length : (n || 0)
  for (let i = n; i < this.length; i++) {
    if (item === this[i]) {
      flag = true;
    }
  }
  return flag;
}
console.log(arr.includes1(2));

上一篇:

下一篇: