Array 数组总结

尽可能的全面

数组实例方法

通过控制台Object.getOwnPropertyNames(Array.prototype)获得浏览器支持的方法,不同浏览器之间有些差异:

  • chrome(68.0.3440.106): 31种 "length, constructor, concat, find, findIndex, pop, push, shift, unshift, slice, splice, includes, indexOf, keys, entries, forEach, filter, map, every, some, reduce, reduceRight, toString, toLocaleString, join, reverse, sort, lastIndexOf, copyWithin, fill, values"

  • firefox(61.0.2): 32种 "length, toSource, toString, toLocaleString, join, reverse, sort, push, pop, shift, unshift, splice, concat, slice, lastIndexOf, indexOf, forEach, map, filter, reduce, reduceRight, some, every, find, findIndex, copyWithin, fill, entries, keys, values, includes, constructor"

  • ie11(11.0.9600.17843): 23种 "constructor, push, concat, join, pop, reverse, shift, slice, sort, splice, toLocaleString, toString, unshift, indexOf, every, filter, forEach, lastIndexOf, map, reduce, reduceRight, some, length"

会改变自身值的方法 (斜体为es6方法)

pop, push, reverse, shift, sort, splice, unshift, copyWithin, fill

pop

array.pop() 删除一个数组中的最后的一个元素,并且返回这个元素。

var arr = ['blue', 'red']
var ret = arr.pop() // ret = 'red'      arr = ['blue']

push

array.push(element1, …, elementN) 添加一个或者多个元素到数组末尾,并且返回数组新的长度。

var arr = ['blue', 'red']
var ret = arr.push('green') // ret = 3      arr = ['blue', 'red', 'green']

reverse

array.reverse() 颠倒数组中元素的位置,返回数组本身

var arr = ['blue', 'red']
var ret = arr.reverse() // ret = ['red', 'blue']       ret === arr

shift

array.shift() 删除数组中第一个元素,并且返回这个元素

var arr = ['blue', 'red', 'green']
var ret = arr.shift() // ret = 'blue'      arr = ['red', 'green']

sort

array.sort([comparefn]) 给数组排序,并且返回数组本身

var arr = ['你', '好', '前', '端']
var ret = arr.sort(function (a, b) {
    return a.localeCompare(b)   // 按照拼音排序
})  // ret = ["端", "好", "你", "前"]     ret === arr

splice

array.splice(start,deleteCount[, item1[, item2[, …]]]) 用来新元素替换旧元素的方式来修改数组,返回删除元素组成的数组,或空数组

var arr = ['blue', 'red', 'green']
var ret = arr.splice(1, 2, 'orange') // ret = ['red', 'green']      arr = ['blue', 'orange']

unshift

array.unshift(element1, …, elementN) 在数组开始处插入一些元素(就像是栈底插入),并返回数组新的长度

var arr = ['blue', 'red']
var ret = arr.unshift('green') // ret = 3      arr = ['green', 'blue', 'red']

copyWithin

array.copyWithin(target, start[, end = this.length]) 组内替换,target 被替换的索引,替换元素的索引,返回本身

var arr = ['blue', 'red', 'green']
var ret = arr.copyWithin(0, 1, 2) // ret = ['red', 'red', 'green']      arr === ret

fill

array.fill(value, start[, end = this.length]) 替换数组某个区间的元素,返回本身

var arr = ['blue', 'red', 'green']
var ret = arr.fill('orange', 0, 2) // ret = ['orange', 'orange', 'green']       arr === ret

不会改变自身值的方法 (斜体为es6方法)

concat、join、slice、toString、toLocateString、indexOf、lastIndexOf、includes

参考

MDN Array

JavaScript数组所有API全解密