數(shù)組的實(shí)例屬性和方法
實(shí)例屬性
- length 數(shù)組中元素的個(gè)數(shù), 需要注意的是续徽,通過(guò)索引為數(shù)組賦值會(huì)改變數(shù)組的長(zhǎng)度
let a = [ 1, 2 ]
console.log(a.length) // 2
a[1000] = 1
console.log(a.length) // 10001
實(shí)例方法
區(qū)分?jǐn)?shù)組和Array
數(shù)組是調(diào)用Array后得到的對(duì)象實(shí)例买乃,Array是該實(shí)例的構(gòu)造函數(shù)
該實(shí)例的原型不是Array,而是Array.prototype,即對(duì)象實(shí)例的原型是該對(duì)象實(shí)例的構(gòu)造函數(shù)的
prototype
屬性
let a = []
a.__proto__ === Array.prototype // true
數(shù)組常用方法
數(shù)組的方法都是繼承自原型對(duì)象——
Array.prototype
一般而言铛纬,為了降低編程的復(fù)雜度嫉戚,傾向于使用
unmutable
(不可變)的方法,基于此维贺,對(duì)數(shù)組的實(shí)例方法分類-
返回新數(shù)組的方法
- concat
合并多個(gè)數(shù)組袜啃,返回的新數(shù)組是對(duì)原始數(shù)組的淺拷貝
let e1 = { foo: 'foo' } let e2 = { bar: 'bar' } let a1 = [e1] let a2 = [e2] let a3 = a1.concat(e2) console.log(a3) // [{ foo: 'foo' }, { bar: 'bar' }] e1.foo = 'bar' console.log(a3) // [{ foo: 'bar' }, { bar: 'bar' }]
- entries
返回一個(gè)新的iterator對(duì)象。iterator
對(duì)象有一個(gè)next
方法幸缕,返回值為有value
和done
屬性的對(duì)象群发,done
標(biāo)識(shí)該
iterator
是否已經(jīng)迭代完畢
let a = [1, 2, 3] let it = a.entries it.next() // { value: [0, 1], done: false } it.next() // { value: [1, 2], done: false } it.next() // { value: [2, 3], done: false } it.next() // { value: undefined, done: true }
- every
測(cè)試數(shù)組中是否所有元素都滿足測(cè)試條件,參數(shù)為返回boolean
的測(cè)試函數(shù)
只要有一個(gè)元素不滿足測(cè)試條件发乔,該方法返回false熟妓,否則返回true。
對(duì)空數(shù)組([])調(diào)用會(huì)返回true
[1, 2, 3].every(function(e) { return e > 1 }) // false
- filter
篩選栏尚,參數(shù)為返回boolean
的測(cè)試函數(shù)起愈,返回滿足條件的元素組成的新數(shù)組,若沒(méi)有滿足條件的元素译仗,則返回空數(shù)組
const a = [10, 5, 8] let b = a.every(function(e, index) { return e > 5 }) // [10, 8]
- find
查找抬虽,參數(shù)為返回boolean
的測(cè)試函數(shù),返回第一個(gè)滿足條件的元素纵菌,若沒(méi)有滿足條件的元素阐污,則返回undefined
const a = [ { id: 1 }, { id: 2 }, { id: 3 } ] let b = a.every(function(e, index) { return e.id > 1 }) // { id: 2 }
- findIndex
查找下標(biāo),參數(shù)為返回boolean
的測(cè)試函數(shù)咱圆,返回第一個(gè)滿足條件的元素的下標(biāo)笛辟,若沒(méi)有滿足條件的元素,則返回-1
const a = [ { id: 1 }, { id: 2 }, { id: 3 } ] let b = a.every(function(e, index) { return e.id > 1 }) // 1
- flat
扁平化數(shù)組序苏,參數(shù)為深度(默認(rèn)為1)手幢,根據(jù)深度將所有元素及其子數(shù)組合并為一個(gè)新的數(shù)組。
使用Infinity
作為參數(shù)可以展開(kāi)任意深度的嵌套數(shù)組
const a = [1, 2, [3, 4, [5, 6]]] a.flat(2) // [1, 2, 3, 4, 5, 6] a.flat(Infinity) // [1, 2, 3, 4, 5, 6]
- flatMap
映射+扁平化忱详,參數(shù)為映射函數(shù)围来,即對(duì)映射后的中間數(shù)組調(diào)用flat(1)
const a = [1, 2, [3, 4, [5, 6]]] a.flatMap(function(e) { return e }) // [1, 2, 3, 4, [5, 6]]
- forEach
對(duì)每個(gè)元素執(zhí)行一次映射函數(shù),參數(shù)為映射函數(shù)
const a = [1, 2, 3] a.forEach(function(e) { console.log(e) }) // 1 // 2 // 3
- includes
測(cè)試一個(gè)數(shù)組是否包含指定元素,參數(shù)為待查找元素监透,可選的第二個(gè)參數(shù)為開(kāi)始查找的下標(biāo)桶错,默認(rèn)為0
檢測(cè)-0 和 0,NaN 和 NaN會(huì)返回true
const a = [1, 2, 0, NaN] a.includes(2) // true a.includes(-0) // true a.includes(NaN) // true
- indexOf
查找一個(gè)給定元素的第一個(gè)索引才漆,參數(shù)為待查找元素牛曹,若未找到,則返回-1
這里使用的是嚴(yán)格相等(類似===)
const a = [1, 2, 3] a.indexOf(2) // 1
- join
返回由參數(shù)連接符的數(shù)組元素組成的字符串醇滥,參數(shù)為連接符黎比,默認(rèn)為,
,對(duì)空數(shù)組返回空字符串
const a = [1, 2, 3] a.join('+') // "1+2+3"
- keys
返回包含數(shù)組中每個(gè)索引鍵的iterator
對(duì)象
const a = [1, 2, 3] const it = a.keys() it.next() // { value: 0, done: false } it.next() // { value: 1, done: false } it.next() // { value: 2, done: false } it.next() // { value: undefined, done: true }
lastIndexOf
與indexOf類似鸳玩,區(qū)別是從后往前找map
根據(jù)映射函數(shù)阅虫,返回一個(gè)新數(shù)組,其中的元素是原數(shù)組中每個(gè)元素調(diào)用映射函數(shù)后的返回值
const a = [1, 2, 3] a.map(function(e) { return 2 * e }) // [2, 4, 6]
- reduce
對(duì)數(shù)組中的每個(gè)元素執(zhí)行reduce
函數(shù)不跟,將其結(jié)果返回成單個(gè)值颓帝,參數(shù)為reduce函數(shù)和初始值initialValue
, 第二個(gè)參數(shù)是可選的
reduce
函數(shù)的第一個(gè)參數(shù)是上一次調(diào)用reduce
函數(shù)的返回值,第二個(gè)參數(shù)是當(dāng)前調(diào)用reduce
函數(shù)對(duì)應(yīng)的數(shù)組元素
在第一次執(zhí)行reduce
函數(shù)時(shí)窝革,- 如果提供了
initialValue
购城,則reduce
函數(shù)的accumulator
值為initialValue
,currentValue
為第一個(gè)元素 - 如果沒(méi)有提供
initialValue
虐译,則reduce
函數(shù)的accumulator
值為數(shù)組的第一個(gè)元素瘪板,currentValue
為第二個(gè)元素 - 如果數(shù)組為空,且沒(méi)有提供
initialValue
漆诽,則拋出異常
所以提供初始值是較好的做法
- 如果提供了
const a = [1, 2, 3] a.reduce(function(accumulator, currentValue) { return accumulator + currentValue }) // 6 a.reduce(function(accumulator, currentValue) { return accumulator + currentValue }, 10) // 16
reduceRight
同reduce類似侮攀,區(qū)別在于從右到左開(kāi)始執(zhí)行reduce函數(shù)-
slice
返回一個(gè)新的數(shù)組對(duì)象,長(zhǎng)度由參數(shù)begin
和end
決定- 省略
begin
厢拭,則從0開(kāi)始,若begin
為負(fù)數(shù)供鸠,如-2畦贸,則從倒數(shù)第2個(gè)元素開(kāi)始 - 省略
end
,則默認(rèn)為數(shù)組長(zhǎng)度 - 若
end
在begin
的左邊回季,則返回空數(shù)組
- 省略
[1, 2, 3].slice(1) // [2, 3] [1, 2, 3].slice(-1) // [3] [1, 2, 3].slice(1, 0) // []
- some
測(cè)試是否至少有一個(gè)元素滿足測(cè)試函數(shù)家制,與every類似,區(qū)別在于只要有一個(gè)元素滿足測(cè)試函數(shù)泡一,就返回true
[1, 2, 3].some(function(e) { return e > 1 }) // true [1, 2, 3].every(function(e) { return e > 1 }) // true
- values
與keys類似,只不過(guò)迭代器的value為數(shù)組的元素觅廓,而不是下標(biāo)
- concat
-
修改原數(shù)組的方法
- fill
使用固定的值填充數(shù)組鼻忠,第一個(gè)參數(shù)是待填充的值,起始位置和結(jié)束位置是可選的
類似這種指明起始和結(jié)束位置的參數(shù),不提供結(jié)束參數(shù)則默認(rèn)到數(shù)組的length帖蔓,并且不包括結(jié)束位置
[1, 2, 3].fill(4) // [4, 4, 4]
- pop
刪除數(shù)組最后一個(gè)元素矮瘟,并返回該元素,該方法會(huì)修改原數(shù)組的長(zhǎng)度
[1, 2, 3].pop() // 3
- push
將一個(gè)或多個(gè)元素添加到數(shù)組的末尾, 返回?cái)?shù)組的新長(zhǎng)度
[1, 2, 3].push(4, 5, 7) // 6
- reverse
將一個(gè)數(shù)組中的元素反序
[1, 2, 3].reverse() // [3, 2, 1]
- shift
與pop類似塑娇,區(qū)別是刪除數(shù)組中的第一個(gè)元素澈侠,并返回該元素
[1, 2, 3].shift() // 1
- sort
排序,可傳入排序函數(shù)埋酬,默認(rèn)會(huì)將元素轉(zhuǎn)換成字符串再按照Unicode位點(diǎn)排序- 比較函數(shù)返回
-1哨啃,0,1
写妥,表示小于拳球,等于,大于
- 比較函數(shù)返回
[10, 200, 3, 9, 81].sort() // [10, 200, 3, 81, 9] [10, 200, 3, 9, 81].sort(function(e1, e2) { return e1 - e2 })
- splice
刪除或替換現(xiàn)有元素或者原地添加新的元素來(lái)修改數(shù)組, 返回被刪除元素組成的數(shù)組- 第一個(gè)參數(shù)
start
是操作開(kāi)始的位置 - 第二個(gè)參數(shù)
deleteCount
為要?jiǎng)h除元素的個(gè)數(shù)珍特,若不指定祝峻,則刪除從start
開(kāi)始刪除剩余所有元素,若小于等于零扎筒,則不刪除 - 從第三個(gè)參數(shù)開(kāi)始莱找,為要添加的元素
- 第一個(gè)參數(shù)
let a = [1, 2, 3] a.splice(1) // 返回[2, 3] a = [1] a = [1, 2, 3] a.splice(1, 1) // 返回[2] a = [1, 3] a = [1, 2, 3] a.splice(1, 1, 11) // 返回[2] a = [1, 11, 3]
- unshift
將一個(gè)或多個(gè)元素添加到數(shù)組的開(kāi)頭,并返回該數(shù)組的新長(zhǎng)度
let a = [1, 2, 3] a.unshift(4, 5) // 返回5 a = [4, 5, 1, 2, 3]
- fill