Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

加深理解,手写数组常用方法! #6

Open
sanjings opened this issue Sep 23, 2020 · 0 comments
Open

加深理解,手写数组常用方法! #6

sanjings opened this issue Sep 23, 2020 · 0 comments

Comments

@sanjings
Copy link
Owner

sanjings commented Sep 23, 2020

加深理解,手写数组常用方法

push()

添加内容至数组末尾,参数可0个或多个,返回修改后的长度,会改变原数组

Array.prototype.myPush = function () {
  for (var i = 0; i < arguments.length; i++) {
    this[this.length] = arguments[i]
  }

  return this.length
}

pop()

移除数组最后一项,返回移除的那一项,会改变原数组

Array.prototype.myPop = function () {
  if (!this.length) return undefined;

  var popItem = this[this.length - 1]
  this.length -= 1

  return popItem
}

shift()

移除数组第一项,返回移除的那一项,会改变原数组

Array.prototype.myShift = function () {
  if (!this.length) return undefined;

  var shiftItem = this[0]
  for (var i = 0; i < this.length - 1; i++) {
    this[i] = this[i + 1]
  }
  this.length -= 1

  return shiftItem
}

unshift()

添加内容至数组开头,参数可0个或多个,返回修改后的长度,会改变原数组

Array.prototype.myUnshift = function () {
  for (var i = arguments.length + this.length - 1; i >= 0; i--) {
    if (i > arguments.length - 1) {
      this[i] = this[i - arguments.length]
    } else {
      this[i] = arguments[i]
    }
  }

  return this.length
}

reverse()

反转数组的顺序,返回修改后的数组,会改变原数组

Array.prototype.myReverse = function () {
  var lastIndex = this.length - 1

  for (var i = 0; i < this.length; i++) {
    var temp = this[i]
    this[i] = this[lastIndex]
    this[lastIndex] = temp
    if (i === lastIndex || lastIndex - i === 1) {
      break;
    }
    lastIndex--
  }

  return this
}

concat()

将内容添加到数组的末尾,也能将多个数组连接起来,返回新的数组,不改变原数组

Array.prototype.myConcat = function () {
  var arr = JSON.parse(JSON.stringify(this)),
      toStr = Object.prototype.toString;

  for (var i = 0; i < arguments.length; i++) {
    if (toStr.call(arguments[i]) === '[object Array]') {
      for (var j = 0; j < arguments[i].length; j++) {
        arr[arr.length] = arguments[i][j]
      }
    } else {
      arr[arr.length] = arguments[i]
    }
  }

  return arr
}

forEach()

对数组进行遍历,没有返回值

Array.prototype.myForEach = function (callback) {
  if (!callback) throw new Error('请传入回调函数')

  var context = arguments[1] || window,
      len = this.length;

  for (var i = 0 ; i < len; i++) {
    callback.apply(context, [this[i], i, this])
  }
}

map()

数组“映射”,对数组进行遍历,返回每次函数调用的结果组成的新数组

Array.prototype.myMap = function (callback) {
  if (!callback) throw new Error('请传入回调函数')

  var context = arguments[1] || window,
      len = this.length,
      newArr = [];

  for (var i = 0 ; i < len; i++) {
    newArr.push(callback.apply(context, [this[i], i, this]))
  }

  return newArr
}

filter()

数组“过滤”,对数组进行遍历,返回满足过滤条件的项组成的新数组

Array.prototype.myFilter = function (callback) {
  if (!callback) throw new Error('请传入回调函数')

  var context = arguments[1] || window,
      len = this.length,
      newArr = [];

  for (var i = 0; i < len; i++) {
    if (Boolean(callback.apply(context, [this[i], i, this]))) {
      newArr.push(this[i])
    }
  }

  return newArr
}

find()

数组“搜索”,对数组进行遍历,返回第一个满足搜索条件的项,如果都不满足,返回undefined。

Array.prototype.myFind = function (callback) {
  if (!callback) throw new Error('请传入回调函数')

  var context = arguments[1] || window,
      len = this.length;

  for (var i = 0; i < len; i++) {
    if (Boolean(callback.apply(context, [this[i], i, this]))) {
      return this[i]
    }
  }
}

every()

遍历数组,判断数组中每一项都是否满足条件,只有所有项都满足条件,才会返回true,否则返回false

Array.prototype.myEvery = function (callback) {
  if (!callback) throw new Error('请传入回调函数')

  var context = arguments[1] || window,
      len = this.length;

  for (var i = 0; i < len; i++) {
    if (!Boolean(callback.apply(context, [this[i], i, this]))) {
      return false
    }
  }

  return true
}

some()

遍历数组,判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true,都不满足返回false

Array.prototype.mySome = function (callback) {
  if (!callback) throw new Error('请传入回调函数')

  var context = arguments[1] || window,
      len = this.length;

  for (var i = 0; i < len; i++) {
    if (Boolean(callback.apply(context, [this[i], i, this]))) {
      return true
    }
  }

  return false
}

reduce()

遍历数组,累加计算数组的每一项,返回计算总和

Array.prototype.myReduce = function (callback, initialValue) {
  if (!callback) throw new Error('请传入回调函数')

  var total = 0,
      i = 0,
      len = this.length;

  if (typeof initialValue === 'undefined') {
    total = this[0]
    i = 1
  } else {
    total = initialValue
  }

  for (i; i < len; i++) {
    total = callback(total, this[i], i, this)
  }

  return total
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant