关于javascript:自实现数组的各种方法

concat() 办法用于合并两个或多个数组。此办法不会更改现有数组,而是返回一个新数组。

Array.prototype.myConcat = function () {
  let result = []
  
  result.push(...this)
  
  for(let b of arguments) {
    if(b instanceof Array) {
      for(let item of b) {
        result.push(item)
      }
    }
    else {
      result.push(b)
    }
  }
  return result
}

copyWithin() 办法浅复制数组的一部分到同一数组中的另一个地位,并返回它,不会扭转原数组的长度。

Array.prototype.myCopyWithin = function(target, start = 0, end = this.length) {
  (end > this.length) && (end = this.length)
  if(start > end || target > end) return false
 let copy = []
  while(start <= end) {
    copy.push(this[start])
    start++
  }
  let index = 0
 while (target <= copy.length) {
    this[target] = copy[index]
    index++
    target++
  }
  return this
}

every() 办法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。

Array.prototype.myEvery = function (fn) {
  for(let a of this) {
    for(let a=0; a< this.length; a++) {
      if(!fn(this[a],a,this)) {
        return false
      }
    }
    return true
    }
}

fill() 办法用一个固定值填充一个数组中从起始索引到终止索引内的全副元素。不包含终止索引。

Array.prototype.myFill = function(target, start = 0, end = this.length) {
  (end > this.length) && (end = this.length)
  if(start > end || target > end) return false
  while (start <= end) {
    this[start] = target
    start++
  }
  return this
}

filter() 办法创立一个新数组, 其蕴含通过所提供函数实现的测试的所有元素。

Array.prototype.myFilter = function(fn) {
  let result = []
  for(let a=0; a< this.length; a++) {
    if(fn(this[a],a,this)) {
      result.push(this[a])
    }
  }
  return result
}

find() 办法返回数组中满足提供的测试函数的第一个元素的值。否则返回 [undefined]。

Array.prototype.myFind = function(fn) {
    for(let a=0; a< this.length; a++) {
        if(fn(this[a],a,this)) {
          return this[a]
        }
      }
    return undefined
 }

findIndex()办法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。

Array.prototype.myFindIndex = function(fn) {
  for(let a=0; a< this.length; a++) {
    if(fn(this[a],a,this)) {
      return a
    }
  }
  return -1
}

includes() 办法用来判断一个数组是否蕴含一个指定的值,依据状况,如果蕴含则返回 true,否则返回false。

Array.prototype.myIncludes = function (value, index = 0) {
  if(index < 0) {
    index = Math.max(index + this.length,0)
  }
  while (index < this.length) {
    if(this[index] === value) {
      return true
    }
    index++
  }
  return false
}

indexOf()办法返回在数组中能够找到一个给定元素的第一个索引,如果不存在,则返回-1。

Array.prototype.myIndexOf = function (value, index = 0) {
  if(index < 0) {
    index = Math.max(index + this.length,0)
  }
  while (index < this.length) {
    if(this[index] === value) {
      return index
    }
    index++
  }
  return -1
}

join() 办法将一个数组(或一个[类数组对象])的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个我的项目,那么将返回该我的项目而不应用分隔符。

如果元素是undefined或者null, 则会转化成空字符串。

Array.prototype.myJoin = function (separator = ',') {
  let result = ''
 for(let a = 0; a < this.length; a++) {
    if(this[a] == null || this[a] == undefined) {
      result += ''
    } else {
      result += this[a]
    }

    if(a < this.length -1) {
      result += separator
    }
  }
  return result
}

lastIndexOf() 办法返回指定元素(也即无效的 JavaScript 值或变量)在数组中的最初一个的索引,如果不存在则返回 -1。从数组的前面向前查找,从 fromIndex 处开始。

Array.prototype.myLastIndexOf = function (value, fromIndex= 0) {
  let index = this.length-1
  if(fromIndex < 0) {
    fromIndex = Math.max(index+this.length, 0)
  }
  while (index >= fromIndex) {
    if(this[index] === value) {
      return index
    }
    index--
  }
  return -1
}

pop()办法从数组中删除最初一个元素,并返回该元素的值。此办法更改数组的长度。

Array.prototype.myPop = function () {
  let result
  if(this.length == 0) {
    return undefined
 } else {
    result = this[this.length-1]
    this.length = this.length - 1
    return result
  }
}

push() 办法将一个或多个元素增加到数组的开端,并返回该数组的新长度。

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

后续待更

【腾讯云】轻量 2核2G4M,首年65元

阿里云限时活动-云数据库 RDS MySQL  1核2G配置 1.88/月 速抢

本文由乐趣区整理发布,转载请注明出处,谢谢。

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据