有趣的JavaScript原生數(shù)組函數(shù)

2018-06-16 19:18 更新

在JavaScript中,可以通過兩種方式創(chuàng)建數(shù)組,構(gòu)造函數(shù)和數(shù)組直接量, 其中后者為首選方法。數(shù)組對(duì)象繼承自Object.prototype,對(duì)數(shù)組執(zhí)行typeof操作符返回‘object’而不是‘a(chǎn)rray’。然而執(zhí)行[] instanceof Array返回true。此外,還有類數(shù)組對(duì)象是問題更復(fù)雜,如字符串對(duì)象,arguments對(duì)象。arguments對(duì)象不是Array的實(shí)例,但卻有個(gè)length屬性,并且值能通過索引獲取,所以能像數(shù)組一樣通過循環(huán)操作。

在本文中,我將復(fù)習(xí)一些數(shù)組原型的方法,并探索這些方法的用法。

  • 循環(huán).forEach
  • 斷言.some.every
  • .join.concat的區(qū)別
  • 棧和隊(duì)列.pop,.push.shift.unshift
  • 模型映射.map
  • 查詢.filter
  • 排序.sort
  • 計(jì)算.reduce.reduceRight
  • 復(fù)制.slice
  • 萬能的.splice
  • 查找.indexOf
  • in操作符
  • 走進(jìn).reverse

如果你想測(cè)試上面的例子,您可以復(fù)制并粘貼到您的瀏覽器的控制臺(tái)中。

循環(huán).forEach

這是JavaScript原生數(shù)組方法中最簡(jiǎn)單的方法。不用懷疑,IE7和IE8不支持此方法。

forEach方法需要一個(gè)回調(diào)函數(shù),數(shù)組內(nèi)的每個(gè)元素都會(huì)調(diào)用一次此方法,此方法需要三個(gè)參數(shù)如下:

  • value 當(dāng)前操作的數(shù)組元素
  • 當(dāng)前操作元素的數(shù)組索引
  • array 當(dāng)前數(shù)組的引用

此外,可以傳遞可選的第二個(gè)參數(shù),作為每個(gè)調(diào)用函數(shù)的上下文(this)。

['_', 't', 'a', 'n', 'i', 'f', ']'].forEach(function (value, index, array) {
    this.push(String.fromCharCode(value.charCodeAt() + index + 2))
}, out = [])

out.join('')
// <- 'awesome'

.join函數(shù)我將在下文提及,上面例子中,它將數(shù)組中的不同元素拼接在一起,類似于如下的效果:out[0] + '' + out[1] + '' + out[2] + '' + out[n]

我們不能用break中斷forEach循環(huán),拋出異常是不明智的方法。幸運(yùn)的是,我們有其他的方法中斷操作。

斷言.some.every

如果你曾經(jīng)用過.NET的枚舉,這些方法的名字和.Any(x => x.IsAwesome).All(x => x.IsAwesome)非常相似。

這些方法和.forEach類似,需要一個(gè)包含value,index,和array三個(gè)參數(shù)的回調(diào)函數(shù),并且也有一個(gè)可選的第二個(gè)上下文參數(shù)。MDN對(duì).some的描述如下:

some將會(huì)給數(shù)組里的每一個(gè)元素執(zhí)行一遍回調(diào)函數(shù),直到有一個(gè)回調(diào)函數(shù)返回true位置。如果找到目標(biāo)元素,some立即返回true,否則some返回false。回調(diào)函數(shù)只對(duì)已經(jīng)指定值的數(shù)組索引執(zhí)行;它不會(huì)對(duì)已刪除的或未指定值的元素執(zhí)行。

max = -Infinity
satisfied = [10, 12, 10, 8, 5, 23].some(function (value, index, array) {
    if (value > max) max = value
    return value < 10
})

console.log(max)
// <- 12

satisfied
// <- true

注意,當(dāng)回調(diào)函數(shù)的value < 10 條件滿足時(shí),中斷函數(shù)循環(huán)。.every的工作行為類似,但回調(diào)函數(shù)要返回false而不是true。

.join.concat的區(qū)別

.join方法經(jīng)常和.concat混淆。.join(分隔符)方法創(chuàng)建一個(gè)字符串,會(huì)將數(shù)組里面每個(gè)元素用分隔符連接。如果沒有提供分隔符,默認(rèn)的分隔符為“,”。.concat方法創(chuàng)建一個(gè)新數(shù)組,其是對(duì)原數(shù)組的淺拷貝(注意是淺拷貝哦)。

  • .concat 的標(biāo)志用法:array.concat(val, val2, val3, valn)
  • .concat 返回一個(gè)新書組
  • array.concat()沒有參數(shù)的情況下,會(huì)返回原數(shù)組的淺拷貝

淺拷貝意味著新數(shù)組和原數(shù)組保持相同的對(duì)象引用,這通常是好事。例如:

var a = { foo: 'bar' }
var b = [1, 2, 3, a]
var c = b.concat()

console.log(b === c)
// <- false

b[3] === a && c[3] === a
// <- true

棧和隊(duì)列.pop,.push,.shift.unshift

每個(gè)人都知道向數(shù)組添加元素用.push。但你知道一次可以添加多個(gè)元素嗎?如下[].push('a', 'b', 'c', 'd', 'z')。

.pop方法和.push成對(duì)使用,它返回?cái)?shù)組的末尾元素并將元素從數(shù)組移除。如果數(shù)組為空,返回void 0(undefined)。使用.push.pop我們能輕易模擬出LIFO(后進(jìn)先出或先進(jìn)后出)棧。

function Stack () {
    this._stack = []
}

Stack.prototype.next = function () {
    return this._stack.pop()
}

Stack.prototype.add = function () {
    return this._stack.push.apply(this._stack, arguments)
}

stack = new Stack()
stack.add(1,2,3)

stack.next()
// <- 3

相反,我們可以用.unshift.shift模擬FIFO(先進(jìn)先出)隊(duì)列。

function Queue () {
    this._queue = []
}

Queue.prototype.next = function () {
    return this._queue.shift()
}

Queue.prototype.add = function () {
    return this._queue.unshift.apply(this._queue, arguments)
}

queue = new Queue()
queue.add(1,2,3)

queue.next()
// <- 1

.shift.pop能很容易遍歷數(shù)組元素,并做一些操作。

list = [1,2,3,4,5,6,7,8,9,10]

while (item = list.shift()) {
    console.log(item)
}

list
// <- []

模型映射.map

map 方法會(huì)給原數(shù)組中的每個(gè)元素(必須有值)都調(diào)用一次 callback 函數(shù).callback 每次執(zhí)行后的返回值組合起來形成一個(gè)新數(shù)組。callback函數(shù)只會(huì)在有值的索引上被調(diào)用; 那些從來沒被賦過值或者使用delete刪除的索引則不會(huì)被調(diào)用?!狹DN

Array.prototype.map方法和上面我們提到的.forEach.some.every有相同的參數(shù):.map(fn(value, index, array), thisArgument)。

values = [void 0, null, false, '']
values[7] = void 0
result = values.map(function(value, index, array){
    console.log(value)
    return value
})

// <- [undefined, null, false, '', undefined × 3, undefined]

undefined × 3 值解釋.map不會(huì)在沒被賦過值或者使用delete刪除的索引上調(diào)用,但他們?nèi)匀槐话诮Y(jié)果數(shù)組中。map在遍歷或改變數(shù)組方面非常有用,如下所示:

// 遍歷
[1, '2', '30', '9'].map(function (value) {
    return parseInt(value, 10)
})
// 1, 2, 30, 9

[97, 119, 101, 115, 111, 109, 101].map(String.fromCharCode).join('')
// <- 'awesome'

// 一個(gè)映射新對(duì)象的通用模式
items.map(function (item) {
    return {
        id: item.id,
        name: computeName(item)
    }
})

查詢.filter

filter對(duì)每個(gè)數(shù)組元素執(zhí)行一次回調(diào)函數(shù),并返回一個(gè)由回調(diào)函數(shù)返回true的元素 組成的新數(shù)組?;卣{(diào)函數(shù)只會(huì)對(duì)已經(jīng)指定值的數(shù)組項(xiàng)調(diào)用。

用法例子:.filter(fn(value, index, array), thisArgument)。把它想象成.Where(x => x.IsAwesome) LINQ expression(如果你熟悉C#),或者SQL語句里面的WHERE??紤]到.filter僅返回callback函數(shù)返回真值的值,下面是一些有趣的例子。沒有傳遞給回調(diào)函數(shù)測(cè)試的元素被簡(jiǎn)單的跳過,不會(huì)包含進(jìn)返回的新書組里。

[void 0, null, false, '', 1].filter(function (value) {
    return value
})
// <- [1]

[void 0, null, false, '', 1].filter(function (value) {
    return !value
})
// <- [void 0, null, false, '']

排序.sort(比較函數(shù))

如果未提供比較函數(shù),元素會(huì)轉(zhuǎn)換為字符串,并按字典許排列。例如,在字典序里,“80”排在“9”之前,但實(shí)際上我們希望的是80在9之后(數(shù)字排序)。

像大部分排序函數(shù)一樣,Array.prototype.sort(fn(a,b))需要一個(gè)包含兩個(gè)測(cè)試參數(shù)的回調(diào)函數(shù),并且要產(chǎn)生一下三種返回值之一:

  • 如果ab前,則返回值小于零
  • 如果ab是等價(jià)的,則返回值等于零
  • 如果ab后,則返回值大于零

代碼

[9,80,3,10,5,6].sort()
// <- [10, 3, 5, 6, 80, 9]

[9,80,3,10,5,6].sort(function (a, b) {
    return a - b
})
// <- [3, 5, 6, 9, 10, 80]

計(jì)算.reduce.reduceRight

首先reduce函數(shù)不是很好理解,.reduce從左到右而.reduceRight從右到左循環(huán)遍歷數(shù)組,每次調(diào)用接收目前為止的部分結(jié)果和當(dāng)前遍歷的值。

兩種方法都有如下典型用法:.reduce(callback(previousValue, currentValue, index, array), initialValue)。

previousValue是最后被調(diào)用的回調(diào)函數(shù)的返回值,initialValue是開始時(shí)previousValue被初始化的值。currentValue 是當(dāng)前被遍歷的元素值,index是當(dāng)前元素在數(shù)組中的索引值。array是對(duì)調(diào)用.reduce數(shù)組的簡(jiǎn)單引用。

一個(gè)典型的用例,使用.reduce的求和函數(shù)。

Array.prototype.sum = function () {
    return this.reduce(function (partial, value) {
        return partial + value
    }, 0)
};

[3,4,5,6,10].sum()
// <- 28

上面提到如果想把數(shù)組連成一個(gè)字符串,可以使用.join。當(dāng)數(shù)組的值是對(duì)象的情況下,除非對(duì)象有能返回其合理值的valueoftoString方法,否則.join的表現(xiàn)和你期望的不一樣。然而,我們可以使用.reduce作為對(duì)象的字符串生成器。

function concat (input) {
    return input.reduce(function (partial, value) {
        if (partial) {
            partial += ', '
        }
        return partial + value
    }, '')
}

concat([
    { name: 'George' },
    { name: 'Sam' },
    { name: 'Pear' }
])
// <- 'George, Sam, Pear'

復(fù)制.slice

.concat類似,調(diào)用.slice缺省參數(shù)時(shí),返回原數(shù)組的淺拷貝。slice函數(shù)需要兩個(gè)參數(shù),一個(gè)是開始位置和一個(gè)結(jié)束位置。

Array.prototype.slice能被用來將類數(shù)組對(duì)象轉(zhuǎn)換為真正的數(shù)組。

Array.prototype.slice.call({ 0: 'a', 1: 'b', length: 2 })
// <- ['a', 'b']

這對(duì).concat不適用,因?yàn)樗鼤?huì)用數(shù)組包裹類數(shù)組對(duì)象。

Array.prototype.concat.call({ 0: 'a', 1: 'b', length: 2 })
// <- [{ 0: 'a', 1: 'b', length: 2 }]

除此之外,另一個(gè)常見用途是從參數(shù)列表中移除最初的幾個(gè)元素,并將類數(shù)組對(duì)象轉(zhuǎn)換為真正的數(shù)組。

function format (text, bold) {
    if (bold) {
        text = '<b>' + text + '</b>'
    }
    var values = Array.prototype.slice.call(arguments, 2)

    values.forEach(function (value) {
        text = text.replace('%s', value)
    })

    return text
}

format('some%sthing%s %s', true, 'some', 'other', 'things')
// <- <b>somesomethingother things</b>

萬能的.splice

.splice是我最喜歡的原生數(shù)組函數(shù)之一。它允許你刪除元素,插入新元素,或在同一位置同時(shí)進(jìn)行上述操作,而只使用一個(gè)函數(shù)調(diào)用。注意和.concat.slice不同的是.splice函數(shù)修改原數(shù)組。

var source = [1,2,3,8,8,8,8,8,9,10,11,12,13]
var spliced = source.splice(3, 4, 4, 5, 6, 7)

console.log(source)
// <- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ,13]

spliced
// <- [8, 8, 8, 8]

你可能已經(jīng)注意到,它也返回被刪除的元素。如果你想遍歷已經(jīng)刪除的數(shù)組時(shí)這可能會(huì)派上用場(chǎng)。

var source = [1,2,3,8,8,8,8,8,9,10,11,12,13]
var spliced = source.splice(9)

spliced.forEach(function (value) {
    console.log('removed', value)
})
// <- removed 10
// <- removed 11
// <- removed 12
// <- removed 13

console.log(source)
// <- [1, 2, 3, 8, 8, 8, 8, 8, 9]

查找.indexOf

通過.indexOf,我們可以查找數(shù)組元素的位置。如果沒有匹配元素則返回-1。我發(fā)現(xiàn)我用的很多的一個(gè)模式是連續(xù)比較,例如a === 'a' || a === 'b' || a === 'c',或者即使只有兩個(gè)結(jié)果的比較。在這種情況下,你也可以使用.indexOf,像這樣:['a', 'b', 'c'].indexOf(a) !== -1。

注意這對(duì)指向同一個(gè)引用的對(duì)象同樣適用。第二個(gè)參數(shù)是開始查詢的起始位置。

var a = { foo: 'bar' }
var b = [a, 2]

console.log(b.indexOf(1))
// <- -1

console.log(b.indexOf({ foo: 'bar' }))
// <- -1

console.log(b.indexOf(a))
// <- 0

console.log(b.indexOf(a, 1))
// <- -1

b.indexOf(2, 1)
// <- 1

如果你想從后向前搜索,.lastIndexOf能派上用場(chǎng)。

in操作符

在面試中新手容易犯的錯(cuò)誤是混淆.indexOfin操作符,如下:

var a = [1, 2, 5]

1 in a
// <- true, 但因?yàn)?2!

5 in a
// <- false

問題的關(guān)鍵是in操作符檢索對(duì)象的鍵而非值。當(dāng)然,這在性能上比.indexOf快得多。

var a = [3, 7, 6]

1 in a === !!a[1]
// <- true

in操作符類似于將鍵值轉(zhuǎn)換為布爾值。!!表達(dá)式通常被開發(fā)者用來雙重取非一個(gè)值(轉(zhuǎn)化為布爾值)。實(shí)際上相當(dāng)于強(qiáng)制轉(zhuǎn)換為布爾值,任何為真的值被轉(zhuǎn)為true,任何為假的值被轉(zhuǎn)換為false

走進(jìn).reverse

這方法將數(shù)組中的元素翻轉(zhuǎn)并替換原來的元素。

var a = [1, 1, 7, 8]

a.reverse()
// [8, 7, 1, 1]

和復(fù)制不同的是,數(shù)組本身被更改。在以后的文章中我將展開對(duì)這些概念的理解,去看看如何創(chuàng)建一個(gè)庫,如Underscore或Lo-Dash。

相關(guān)文章

譯者注

本文為翻譯文章,原文為“Fun with JavaScript Native Array Functions”。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)