關于JavaScript 數(shù)組你應該知道的事情(推薦)
首先做一個粗體聲明:循環(huán)經(jīng)常是無用的,并且使得代碼很難閱讀。
當談到迭代一個數(shù)組的時候,無論你想去查找元素,排序或者任何其他的事,都有可能存在一個數(shù)組的方法供你使用。
然而,盡管它們有用,但其中一些仍然不被人了解。我會努力為你展示一些有用的方法。把這篇文章當做對 JavaScript 數(shù)組方法的指引吧。
注意: 在開始之前,不得不了解一件事:我比較偏愛函數(shù)式編程。所以我傾向于使用的方法不會直接改變原來的數(shù)組。這種方法,我避免了副作用。我不是說不應該改變數(shù)組,但至少要了解那些方法會改變,那些會有副作用。副作用導致不想要的改變,而不想要的改變帶來bugs!
了解到這里,我們可以開始正文了。
必不可少的
當跟數(shù)組打交道時,有四件事你應該清楚:map,filter,reduce 和 展開操作符。它們富有力量。
map
你可以在很多種情況下使用它?;镜?,每次你需要修改數(shù)組的元素時,考慮使用 map。
它接受一個參數(shù):一個方法,在每一個數(shù)組元素上調用。然后返回一個新的數(shù)組,所以沒有副作用。
const numbers = [1, 2, 3, 4] const numbersPlusOne = numbers.map(n => n + 1) // 每個元素 +1 console.log(numbersPlusOne) // [2, 3, 4, 5]
你也能創(chuàng)建一個新數(shù)組,用于保留對象的一個特殊屬性:
const allActivities = [
{ title: 'My activity', coordinates: [50.123, 3.291] },
{ title: 'Another activity', coordinates: [1.238, 4.292] },
// etc.
]
const allCoordinates = allActivities.map(activity => activity.coordinates)
console.log(allCoordinates) // [[50.123, 3.291], [1.238, 4.292]]
所以,請記住,當你需要去轉換數(shù)組時,考慮使用map。
filter
這個方法的名字在這里十分準確的:當你想去過濾數(shù)組的時候使用它。
如同map所做,它接受一個函數(shù)作為它的唯一參數(shù),在數(shù)組的每個元素上調用。這個方法返回一個布爾值:
- true 如果你需要在數(shù)組中保留元素
- false 如果你不想保留它
接著你會得到一個帶有你想要保留的元素的新數(shù)組。
舉個例子,你可以在數(shù)組中只保留奇數(shù):
const numbers = [1, 2, 3, 4, 5, 6] const oddNumbers = numbers.filter(n => n % 2 !== 0) console.log(oddNumbers) // [1, 3, 5]
或者你可以在數(shù)組中移除特殊的項:
const participants = [
{ id: 'a3f47', username: 'john' },
{ id: 'fek28', username: 'mary' },
{ id: 'n3j44', username: 'sam' },
]
function removeParticipant(participants, id) {
return participants.filter(participant => participant.id !== id)
}
console.log(removeParticipant(participants, 'a3f47')) // [{ id: 'fek28', username: 'mary' }, { id: 'n3j44', username: 'sam' }];
reduce
個人認為是最難理解的方法。但是如果你一旦掌握它,很多瘋狂的事情你都可以用它做到。
基本地, reduce 使用有值的數(shù)組然后組合成一個新的值。它接受兩個參數(shù),一個回調方法就是我們的 reducer 和一個可選的初始化的值(默認是數(shù)組的第一個項)。這個 reducer 自己使用四個參數(shù):
- 累計:在你的 reducer 中累積的返回值
- 當前數(shù)組的值
- 當前索引
- 當前調用 reduce 的數(shù)組
大多數(shù)時候,你只需要使用前兩個參數(shù):累計值和當前值。
拋開這些理論。來看看常見的一個 reduce 的例子。
const numbers = [37, 12, 28, 4, 9] const total = numbers.reduce((total, n) => total + n) console.log(total) // 90
在第一個遍歷時,這個累計值,也就是 total,使用了初始化為 37 的值。它返回的值是 37 + n 并且 n 等于 12,因此得到 49.在第二次遍歷時,累加值是 49,返回值是 49 + 28 = 77。如此繼續(xù)直到第四次。
reduce 是很強大的,你可以實際使用它去構建很多數(shù)組的方法,比如 map 或者 filter:
const map = (arr, fn) => {
return arr.reduce((mappedArr, element) => {
return [...mappedArr, fn(element)]
}, [])
}
console.log(map([1, 2, 3, 4], n => n + 1)) // [2, 3, 4, 5]
const filter = (arr, fn) => {
return arr.reduce((filteredArr, element) => {
return fn(element) ? [...filteredArr] : [...filteredArr, element]
}, [])
}
console.log(filter([1, 2, 3, 4, 5, 6], n => n % 2 === 0)) // [1, 3, 5]
根本上看,我們給 reduce 一個初始默認值 []:我們的累計值。對于 map,我們運行一個方法,它的結果是累加到最后,多虧了 展開操作符(不必擔心,后面討論)。對于 filter,幾乎是相似的,除了我們在元素上運行過濾函數(shù)。如果返回 true,我們返回前一個數(shù)組,否則在數(shù)組最后添加當前元素。
我們來看一個更高級的例子:深度展開數(shù)組,也就是說把 [1, 2, 3, [4, [[[5, [6, 7]]]], 8]] 樣的數(shù)組轉換成 [1, 2, 3, 4, 5, 6, 7, 8] 樣的。
function flatDeep(arr) {
return arr.reduce((flattenArray, element) => {
return Array.isArray(element)
? [...flattenArray, ...flatDeep(element)]
: [...flattenArray, element]
}, [])
}
console.log(flatDeep([1, 2, 3, [4, [[[5, [6, 7]]]], 8]])) // [1, 2, 3, 4, 5, 6, 7, 8]
這個例子有點像 map,除了我們用到了遞歸。我不想去解釋這個用法,它超出了這篇文章的范圍。但是,如果你想了解更多的關于遞歸的知識,請參考這篇優(yōu)質的文章。
展開操作(ES2015)
我知道這不是一個方法。但是,在處理數(shù)組時,使用展開操作可以幫助你做很多事情。事實上,你可以在另一個數(shù)組中使用它展開一個數(shù)組的值。從這一點來說,你可以復制一個數(shù)組,或者連接多個數(shù)組。
const numbers = [1, 2, 3] const numbersCopy = [...numbers] console.log(numbersCopy) // [1, 2, 3] const otherNumbers = [4, 5, 6] const numbersConcatenated = [...numbers, ...otherNumbers] console.log(numbersConcatenated) // [1, 2, 3, 4, 5, 6]
注意::展開操作符對原數(shù)組做了一次淺拷貝。但什么是 淺拷貝?🤔
額,淺拷貝是盡可能少的復制原數(shù)組。當你有一個數(shù)組包含數(shù)字,字符串或者布爾值(基本類型),它們是沒問題的,這些值被真正復制。然而,對于 對象和數(shù)組 而言,這是不同的。只有 對原值的引用 會被復制!因此,如果你創(chuàng)建一個包含對象的數(shù)組的淺拷貝,然后在拷貝的數(shù)組中修改了對象,它也會修改原數(shù)組的對象,因為它們是 同一個引用。
const arr = ['foo', 42, { name: 'Thomas' }]
let copy = [...arr]
copy[0] = 'bar'
console.log(arr) // No mutations: ["foo", 42, { name: "Thomas" }]
console.log(copy) // ["bar", 42, { name: "Thomas" }]
copy[2].name = 'Hello'
console.log(arr) // /!\ MUTATION ["foo", 42, { name: "Hello" }]
console.log(copy) // ["bar", 42, { name: "Hello" }]
所以,如果你想去“真正地”靠譜一個包含對象或者數(shù)組的是維護組,你可以使用 lodash 的方法 cloneDeep。但是不要覺得必須做這樣的事。這里的目標是 意識到事情是如何運作的。
最好了解的
下面你看到的方法,是最好了解一下的,同時它們能幫助你解決某些問題,比如在數(shù)組中搜索一個元素,取出數(shù)組的部分或者更多。
includes(ES2015)
你曾經(jīng)嘗試用過 indexOf 去查找一個數(shù)組中是否存在某個東西嗎?這是一個糟糕的方式對吧?幸運的是,includes 為我們做到了這些。給 includes 一個參數(shù),然后會在數(shù)組里面搜索它,如果一個元素存在的話。
const sports = ['football', 'archery', 'judo']
const hasFootball = sports.includes('football')
console.log(hasFootball) // true
concat
concat 方法可以用來合并兩個或者更多的數(shù)組。
const numbers = [1, 2, 3]
const otherNumbers = [4, 5, 6]
const numbersConcatenated = numbers.concat(otherNumbers)
console.log(numbersConcatenated) // [1, 2, 3, 4, 5, 6]
// You can merge as many arrays as you want
function concatAll(arr, ...arrays) {
return arr.concat(...arrays)
}
console.log(concatAll([1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12])) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
forEach
無論何時你想為數(shù)組的每個元素執(zhí)行一些事情時,可以使用 forEach。它使用一個函數(shù)作為參數(shù),然后給它三個參數(shù):當前值,索引,和當前數(shù)組。
const numbers = [1, 2, 3, 4, 5] numbers.forEach(console.log) // 1 0 [ 1, 2, 3 ] // 2 1 [ 1, 2, 3 ] // 3 2 [ 1, 2, 3 ]
indexOf
這個用來在給定的數(shù)組中找出第一個被發(fā)現(xiàn)的元素的索引。 indexOf 也廣泛用于檢查元素是否在一個數(shù)組中。不過老實說,我如今已經(jīng)不這樣使用了。
const sports = ['football', 'archery', 'judo']
const judoIndex = sports.indexOf('judo')
console.log(judoIndex) // 2
find
find 方法十分類似于 filter 方法。你必須提供一個函數(shù)用于測試數(shù)組的元素。然而,find 一旦發(fā)現(xiàn)有一個元素通過測試,就立即停止測試其他元素。不用于 filter,filter 將會迭代整個數(shù)組,無論情況如何。
const users = [
{ id: 'af35', name: 'john' },
{ id: '6gbe', name: 'mary' },
{ id: '932j', name: 'gary' },
]
const user = users.find(user => user.id === '6gbe')
console.log(user) // { id: '6gbe', name: 'mary' }
所以使用 filter,當你想去過濾整個數(shù)組時。使用 find 在當你確定在數(shù)組中找某個唯一元素的時候。
findIndex
這個方法完全跟 find 相同除了它返回第一個發(fā)現(xiàn)元素的索引,而不是直接返回元素。
const users = [
{ id: 'af35', name: 'john' },
{ id: '6gbe', name: 'mary' },
{ id: '932j', name: 'gary' },
]
const user = users.findIndex(user => user.id === '6gbe')
console.log(user) // 1
你或許認為 findIndex 跟 indexOf 是相同的。額……不完全是。indexOf 的第一個元素是基本值(布爾,數(shù)字,字符串,null,undefined或者一個 symbol)而findIndex的第一個元素是一個回調方法。
所以當你需要搜索在數(shù)組中的一個元素的基本值時,使用 indexOf。如果有更復雜的元素,比如object,使用 findIndex。
slice
當你需要取出或者復制數(shù)組的一部分,可以使用 slice。但是注意,像展開操作符一樣, slice 返回部分的淺拷貝!
const numbers = [1, 2, 3, 4, 5] const copy = numbers.slice()
我在文章的開始談到,循環(huán)是沒有什么用的。來用一個例子說明你如何擺脫它。
假設你想去從 API 中去除一定量的聊天記錄里,然后展示它們中的 5 條。有兩種方式實現(xiàn):一種是循環(huán),另一種是 slice。
// 傳統(tǒng)方式
// 用循環(huán)來決定消息的數(shù)量
const nbMessages = messages.length < 5 ? messages.length : 5
let messagesToShow = []
for (let i = 0; i < nbMessages; i++) {
messagesToShow.push(posts[i])
}
// 假設 arr 少于 5 個元素
// slice 將會返回原數(shù)組的整個淺拷貝
const messagesToShow = messages.slice(0, 5)
some
如果你想測試數(shù)組中 至少有一個元素 通過測試,那么可以使用 some。就像是 map,filter,和 find,some 用回調函數(shù)作為參數(shù)。它返回 ture,如果至少一個元素通過測試,返回 true 否則返回 false。
當你處理權限問題的時候,可以使用 some:
const users = [
{
id: 'fe34',
permissions: ['read', 'write'],
},
{
id: 'a198',
permissions: [],
},
{
id: '18aa',
permissions: ['delete', 'read', 'write'],
},
]
const hasDeletePermission = users.some(user =>
user.permissions.includes('delete')
)
console.log(hasDeletePermission) // true
every
類似 some,不同的是 ever 測試了所有的元素是否滿足條件(而不是 至少一個)。
const users = [
{
id: 'fe34',
permissions: ['read', 'write'],
},
{
id: 'a198',
permissions: [],
},
{
id: '18aa',
permissions: ['delete', 'read', 'write'],
},
]
const hasAllReadPermission = users.every(user =>
user.permissions.includes('read')
)
console.log(hasAllReadPermission) // false
flat(ES2019)
這是一個即將到來的招牌方法, 在JavaScript 世界中。大致而言,flat 穿件一個新數(shù)組,通過組合所有的子數(shù)組元素。接受一個參數(shù),數(shù)值類型,代表你想展開的深度。
const numbers = [1, 2, [3, 4, [5, [6, 7]], [[[[8]]]]]] const numbersflattenOnce = numbers.flat() console.log(numbersflattenOnce) // [1, 2, 3, 4, Array[2], Array[1]] const numbersflattenTwice = numbers.flat(2) console.log(numbersflattenTwice) // [1, 2, 3, 4, 5, Array[2], Array[1]] const numbersFlattenInfinity = numbers.flat(Infinity) console.log(numbersFlattenInfinity) // [1, 2, 3, 4, 5, 6, 7, 8]
flatMap(ES2019)
猜猜這個方法干什么?我打賭你可以做到顧名思義。
首先在每個元素上運行一個 mapping 方法。接著一次性展示數(shù)據(jù)。十分簡單!
const sentences = [
'This is a sentence',
'This is another sentence',
"I can't find any original phrases",
]
const allWords = sentences.flatMap(sentence => sentence.split(' '))
console.log(allWords) // ["This", "is", "a", "sentence", "This", "is", "another", "sentence", "I", "can't", "find", "any", "original", "phrases"]
這個例子中,數(shù)組里有一些句子,然而我們想得到所有的單詞。不使用 map 去把所有的句子分割成單詞然后展開數(shù)組,你可以直接使用 flatMap。
與 flatMap 無關的,你可以使用 reduce 方法來計算單詞的數(shù)量(只是展示另一種 reduce 的用法)
const wordsCount = allWords.reduce((count, word) => {
count[word] = count[word] ? count[word] + 1 : 1
return count
}, {})
console.log(wordsCount) // { This: 2, is: 2, a: 1, sentence: 2, another: 1, I: 1, "can't": 1, find: 1, any: 1, original: 1, phrases: 1, }
flatMap 經(jīng)常用于響應式編程,這里有個例子。
join
如果你需要基于數(shù)組元素創(chuàng)建字符串,join 正是你所尋找的。它允許通過鏈接數(shù)組元素來創(chuàng)建一個新的字符串,通過提供的分割符分割。
舉個例子,你可以使用 join 一眼展示活動的參與者:
const participants = ['john', 'mary', 'gary']
const participantsFormatted = participants.join(', ')
console.log(participantsFormatted) // john, mary, gary
下面的例子更真實,在于你想先過濾參與者然后得到他們的名字。
const potentialParticipants = [
{ id: 'k38i', name: 'john', age: 17 },
{ id: 'baf3', name: 'mary', age: 13 },
{ id: 'a111', name: 'gary', age: 24 },
{ id: 'fx34', name: 'emma', age: 34 },
]
const participantsFormatted = potentialParticipants
.filter(user => user.age > 18)
.map(user => user.name)
.join(', ')
console.log(participantsFormatted) // gary, emma
from
這是一個靜態(tài)方法,從類數(shù)組中創(chuàng)建新的數(shù)組,或者像例子中的字符串一樣遍歷對象。當處理 dom 時,這個方法十分有用。
const nodes = document.querySelectorAll('.todo-item') // 這是一個 nodeList 實例
const todoItems = Array.from(nodes) // 現(xiàn)在你能使用 map filter 等等,就像在數(shù)組中那樣!
你曾經(jīng)見到過我們使用 Array 代替數(shù)組實例嗎?這就是問什么 from 被稱作靜態(tài)方法。
接著可以愉快處理這些節(jié)點,比如用 forEach 在每個節(jié)點上注冊事件監(jiān)聽:
todoItems.forEach(item => {
item.addEventListener('click', function() {
alert(`You clicked on ${item.innerHTML}`)
})
})
最好了解突變
下面是其他常見的數(shù)組方法。不同之處在于,它們會修改原數(shù)組。修改數(shù)組并沒有什么錯,最好是你應該有意識去修改它。
對于這些方法,如果你不想去改變原數(shù)組,只能在操作前淺拷貝或者深拷貝。
const arr = [1, 2, 3, 4, 5] const copy = [...arr] // or arr.slice()
sort
是的,sort 修改了原數(shù)組。事實上,在這里進行了數(shù)組元素排序。默認的排序方法把所有的元素轉換成字符串,然后按照字母表排序它們。
const names = ['john', 'mary', 'gary', 'anna'] names.sort() console.log(names) // ['anna', 'gary', 'john', 'mary']
如果你有 Python 背景的話,要小心了。使用 sort 在數(shù)字數(shù)組中不會得到你想要的結果
const numbers = [23, 12, 17, 187, 3, 90] numbers.sort() console.log(numbers) // [12, 17, 187, 23, 3, 90] 🤔。
那么如何對一個數(shù)組排序?額,sort 接受一個函數(shù),一個比較函數(shù)。這個函數(shù)接受兩個參數(shù):第一個元素(我們稱呼為 a)和第二個元素作比較(b)。這兩個元素之間的比較需要返回一個數(shù)字。
- 如果為負,a 排序在 b 之前。
- 如果為正,b 排序在 a 之前。
- 如果是0,沒有任何改變。
那么你可以使用下面的方式排序數(shù)組:
const numbers = [23, 12, 17, 187, 3, 90] numbers.sort((a, b) => a - b) console.log(numbers) // [3, 12, 17, 23, 90, 187]
或者通過最近時間排序:
const posts = [
{
title: 'Create a Discord bot under 15 minutes',
date: new Date(2018, 11, 26),
},
{ title: 'How to get better at writing CSS', date: new Date(2018, 06, 17) },
{ title: 'JavaScript arrays', date: new Date() },
]
posts.sort((a, b) => a.date - b.date) // Substracting two dates returns the difference in millisecond between them
console.log(posts)
// [ { title: 'How to get better at writing CSS',
// date: 2018-07-17T00:00:00.000Z },
// { title: 'Create a Discord bot under 15 minutes',
// date: 2018-12-26T00:00:00.000Z },
// { title: 'Learn Javascript arrays the functional way',
// date: 2019-03-16T10:31:00.208Z } ]
fill
fill 修改或者填充了數(shù)組的所有元素,從開始索引到結束索引,使用一個靜態(tài)值。fill 最有用的作用是使用靜態(tài)值填充一個新數(shù)組。
// Normally I would have called a function that generates ids and random names but let's not bother with that here.
function fakeUser() {
return {
id: 'fe38',
name: 'thomas',
}
}
const posts = Array(3).fill(fakeUser())
console.log(posts) // [{ id: "fe38", name: "thomas" }, { id: "fe38", name: "thomas" }, { id: "fe38", name: "thomas" }]
reverse
這個方法名在這里顯而易見。然而,像留意 sort 那樣,reverse 會反轉數(shù)組的位置。
const numbers = [1, 2, 3, 4, 5] numbers.reverse() console.log(numbers) // [5, 4, 3, 2, 1]
你可以替換的方法
終于,在這個最后的部分,你將發(fā)現(xiàn)改變原數(shù)組的方法,同時可以很容易替換其中一些。我不是說你應該拋棄這些方法。只是想要你意識到一些數(shù)組方法有副作用,并且這里有可選擇的其他方法。
push
處理數(shù)組時這是使用最多的方法。事實上,push 允許你在數(shù)組中添加一個或者多個元素。它也通?;谝粋€舊數(shù)組構建一個新數(shù)組。
const todoItems = [1, 2, 3, 4, 5]
const itemsIncremented = []
for (let i = 0; i < items.length; i++) {
itemsIncremented.push(items[i] + 1)
}
console.log(itemsIncremented) // [2, 3, 4, 5, 6]
const todos = ['Write an article', 'Proofreading']
todos.push('Publish the article')
console.log(todos) // ['Write an article', 'Proofreading', 'Publish the article']
如果你需要像 itemsIncremented 一樣構建一個數(shù)組,很多方法都是機會,像我們的朋友 map,filter或者reduce。事實上我們可以使用 map 同樣做到:
const itemsIncremented = todoItems.map(x => x + 1)
并且如果你需要使用 push,當你要添加新元素的時候,展開操作符為你撐腰。
const todos = ['Write an article', 'Proofreading'] console.log([...todos, 'Publish the article']) // ['Write an article', 'Proofreading', 'Publish the article']
splice
splice 常常用于作為移除某個索引元素的方法。你可以同樣使用 filter 做到。
const months = ['January', 'February', 'March', 'April', ' May'] // With splice months.splice(2, 1) // remove one element at index 2 console.log(months) // ['January', 'February', 'April', 'May'] // Without splice const monthsFiltered = months.filter((month, i) => i !== 3) console.log(monthsFiltered) // ['January', 'February', 'April', 'May']
你可能會想,如果我需要移除多個元素呢?額,使用 slice:
const months = ['January', 'February', 'March', 'April', ' May'] // With splice months.splice(1, 3) // remove thirds element starting at index 1 console.log(months) // ['January', 'May'] // Without splice const monthsFiltered = [...months.slice(0, 1), ...months.slice(4)] console.log(monthsFiltered) // ['January', 'May']
shift
shift 移除數(shù)組的第一個元素然后返回它。從功能上來說,你可以使用 spread/rest 實現(xiàn)。
const numbers = [1, 2, 3, 4, 5] // With shift const firstNumber = numbers.shift() console.log(firstNumber) // 1 console.log(numbers) // [2, 3, 4, 5] // Without shift const [firstNumber, ...numbersWithoutOne] = numbers console.log(firstNumber) // 1 console.log(numbersWithoutOne) // [2, 3, 4, 5]
unshift
Unshift 允許你在數(shù)組開始添加一個或者多個元素。像是 shift, 你可以使用展開操作符做同樣的事:
const numbers = [3, 4, 5] // With unshift numbers.unshift(1, 2) console.log(numbers) // [1, 2, 3, 4, 5] // Without unshift const newNumbers = [1, 2, ...numbers] console.log(newNumbers) // [1, 2, 3, 4, 5]
太長不看版:
- 無論何時你在數(shù)組上操作時,不要使用 for-loop 也不要重復造輪子,你想做的可能已經(jīng)有一個方法在那里。
- 大多數(shù)情況,你應該使用 map,filter,reduce和展開操作符。它們對開發(fā)者來說是最基礎的工具。
- 有許多方法需要了解像 slice,some,flatMap等等。記住它們并且在合適的時候使用它們。
- 副作用導致不想要的改變。要清楚哪些方法會改變你的原始數(shù)組。
- slice 和展開操作符是淺拷貝。因此,對象和子數(shù)組將會共享同一個引用,小心使用它們。
- “舊”的改變數(shù)組的方法可以被新的替換。取決于你想做什么。
以上所述是小編給大家介紹的JavaScript 數(shù)組詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
javascript模擬post提交隱藏地址欄的參數(shù)
想要隱藏地址欄的參數(shù),就只能用javascript模擬post提交,下面是示例代碼,需要的朋友可以看看2014-09-09

