亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

關(guān)于JavaScript 數(shù)組你應(yīng)該知道的事情(推薦)

 更新時間:2019年04月10日 09:35:06   作者:toddmark  
這篇文章主要介紹了JavaScript 數(shù)組,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

首先做一個粗體聲明:循環(huán)經(jīng)常是無用的,并且使得代碼很難閱讀。
當(dāng)談到迭代一個數(shù)組的時候,無論你想去查找元素,排序或者任何其他的事,都有可能存在一個數(shù)組的方法供你使用。

然而,盡管它們有用,但其中一些仍然不被人了解。我會努力為你展示一些有用的方法。把這篇文章當(dāng)做對 JavaScript 數(shù)組方法的指引吧。

注意: 在開始之前,不得不了解一件事:我比較偏愛函數(shù)式編程。所以我傾向于使用的方法不會直接改變原來的數(shù)組。這種方法,我避免了副作用。我不是說不應(yīng)該改變數(shù)組,但至少要了解那些方法會改變,那些會有副作用。副作用導(dǎo)致不想要的改變,而不想要的改變帶來bugs!

了解到這里,我們可以開始正文了。

必不可少的
當(dāng)跟數(shù)組打交道時,有四件事你應(yīng)該清楚:map,filter,reduce 和 展開操作符。它們富有力量。

map

你可以在很多種情況下使用它。基本地,每次你需要修改數(shù)組的元素時,考慮使用 map。
它接受一個參數(shù):一個方法,在每一個數(shù)組元素上調(diào)用。然后返回一個新的數(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]]

所以,請記住,當(dāng)你需要去轉(zhuǎn)換數(shù)組時,考慮使用map。

filter

這個方法的名字在這里十分準(zhǔn)確的:當(dāng)你想去過濾數(shù)組的時候使用它。
如同map所做,它接受一個函數(shù)作為它的唯一參數(shù),在數(shù)組的每個元素上調(diào)用。這個方法返回一個布爾值:

  1. true 如果你需要在數(shù)組中保留元素
  2. 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

個人認(rèn)為是最難理解的方法。但是如果你一旦掌握它,很多瘋狂的事情你都可以用它做到。
基本地, reduce 使用有值的數(shù)組然后組合成一個新的值。它接受兩個參數(shù),一個回調(diào)方法就是我們的 reducer 和一個可選的初始化的值(默認(rèn)是數(shù)組的第一個項)。這個 reducer 自己使用四個參數(shù):

  1. 累計:在你的 reducer 中累積的返回值
  2. 當(dāng)前數(shù)組的值
  3. 當(dāng)前索引
  4. 當(dāng)前調(diào)用 reduce 的數(shù)組

大多數(shù)時候,你只需要使用前兩個參數(shù):累計值和當(dāng)前值。
拋開這些理論。來看看常見的一個 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 是很強(qiáng)大的,你可以實際使用它去構(gòu)建很多數(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 一個初始默認(rèn)值 []:我們的累計值。對于 map,我們運行一個方法,它的結(jié)果是累加到最后,多虧了 展開操作符(不必?fù)?dān)心,后面討論)。對于 filter,幾乎是相似的,除了我們在元素上運行過濾函數(shù)。如果返回 true,我們返回前一個數(shù)組,否則在數(shù)組最后添加當(dāng)前元素。

我們來看一個更高級的例子:深度展開數(shù)組,也就是說把 [1, 2, 3, [4, [[[5, [6, 7]]]], 8]] 樣的數(shù)組轉(zhuǎn)換成 [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,除了我們用到了遞歸。我不想去解釋這個用法,它超出了這篇文章的范圍。但是,如果你想了解更多的關(guān)于遞歸的知識,請參考這篇優(yōu)質(zhì)的文章。

展開操作(ES2015)

我知道這不是一個方法。但是,在處理數(shù)組時,使用展開操作可以幫助你做很多事情。事實上,你可以在另一個數(shù)組中使用它展開一個數(shù)組的值。從這一點來說,你可以復(fù)制一個數(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ù)組做了一次淺拷貝。但什么是 淺拷貝?🤔

額,淺拷貝是盡可能少的復(fù)制原數(shù)組。當(dāng)你有一個數(shù)組包含數(shù)字,字符串或者布爾值(基本類型),它們是沒問題的,這些值被真正復(fù)制。然而,對于 對象和數(shù)組 而言,這是不同的。只有 對原值的引用 會被復(fù)制!因此,如果你創(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ù)組的是維護(hù)組,你可以使用 lodash 的方法 cloneDeep。但是不要覺得必須做這樣的事。這里的目標(biāo)是 意識到事情是如何運作的。

最好了解的

下面你看到的方法,是最好了解一下的,同時它們能幫助你解決某些問題,比如在數(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ù):當(dāng)前值,索引,和當(dāng)前數(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,當(dāng)你想去過濾整個數(shù)組時。使用 find 在當(dāng)你確定在數(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

你或許認(rèn)為 findIndex 跟 indexOf 是相同的。額……不完全是。indexOf 的第一個元素是基本值(布爾,數(shù)字,字符串,null,undefined或者一個 symbol)而findIndex的第一個元素是一個回調(diào)方法。
所以當(dāng)你需要搜索在數(shù)組中的一個元素的基本值時,使用 indexOf。如果有更復(fù)雜的元素,比如object,使用 findIndex。

slice

當(dāng)你需要取出或者復(fù)制數(shù)組的一部分,可以使用 slice。但是注意,像展開操作符一樣, slice 返回部分的淺拷貝!

const numbers = [1, 2, 3, 4, 5]
const copy = numbers.slice()

我在文章的開始談到,循環(huán)是沒有什么用的。來用一個例子說明你如何擺脫它。

假設(shè)你想去從 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])
}

// 假設(shè) arr 少于 5 個元素
// slice 將會返回原數(shù)組的整個淺拷貝
const messagesToShow = messages.slice(0, 5)

some

如果你想測試數(shù)組中 至少有一個元素 通過測試,那么可以使用 some。就像是 map,filter,和 find,some 用回調(diào)函數(shù)作為參數(shù)。它返回 ture,如果至少一個元素通過測試,返回 true 否則返回 false。
當(dāng)你處理權(quán)限問題的時候,可以使用 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 無關(guān)的,你可以使用 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)常用于響應(yī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ù)組,或者像例子中的字符串一樣遍歷對象。當(dāng)處理 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ù)組并沒有什么錯,最好是你應(yīng)該有意識去修改它。

對于這些方法,如果你不想去改變原數(shù)組,只能在操作前淺拷貝或者深拷貝。

const arr = [1, 2, 3, 4, 5]
const copy = [...arr] // or arr.slice()

sort

是的,sort 修改了原數(shù)組。事實上,在這里進(jìn)行了數(shù)組元素排序。默認(rèn)的排序方法把所有的元素轉(zhuǎn)換成字符串,然后按照字母表排序它們。

const names = ['john', 'mary', 'gary', 'anna']
names.sort()
console.log(names) // ['anna', 'gary', 'john', 'mary']

如果你有 Python 背景的話,要小心了。使用 sort 在數(shù)字?jǐn)?shù)組中不會得到你想要的結(jié)果

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ù)字。

  1. 如果為負(fù),a 排序在 b 之前。
  2. 如果為正,b 排序在 a 之前。
  3. 如果是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ù)組的所有元素,從開始索引到結(jié)束索引,使用一個靜態(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 會反轉(zhuǎn)數(shù)組的位置。

const numbers = [1, 2, 3, 4, 5]

numbers.reverse()
console.log(numbers) // [5, 4, 3, 2, 1]

你可以替換的方法

終于,在這個最后的部分,你將發(fā)現(xiàn)改變原數(shù)組的方法,同時可以很容易替換其中一些。我不是說你應(yīng)該拋棄這些方法。只是想要你意識到一些數(shù)組方法有副作用,并且這里有可選擇的其他方法。

push

處理數(shù)組時這是使用最多的方法。事實上,push 允許你在數(shù)組中添加一個或者多個元素。它也通?;谝粋€舊數(shù)組構(gòu)建一個新數(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 一樣構(gòu)建一個數(shù)組,很多方法都是機(jī)會,像我們的朋友 map,filter或者reduce。事實上我們可以使用 map 同樣做到:

const itemsIncremented = todoItems.map(x => x + 1)

并且如果你需要使用 push,當(dāng)你要添加新元素的時候,展開操作符為你撐腰。

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]

太長不看版:

  1. 無論何時你在數(shù)組上操作時,不要使用 for-loop 也不要重復(fù)造輪子,你想做的可能已經(jīng)有一個方法在那里。
  2. 大多數(shù)情況,你應(yīng)該使用 map,filter,reduce和展開操作符。它們對開發(fā)者來說是最基礎(chǔ)的工具。
  3. 有許多方法需要了解像 slice,some,flatMap等等。記住它們并且在合適的時候使用它們。
  4. 副作用導(dǎo)致不想要的改變。要清楚哪些方法會改變你的原始數(shù)組。
  5. slice 和展開操作符是淺拷貝。因此,對象和子數(shù)組將會共享同一個引用,小心使用它們。
  6. “舊”的改變數(shù)組的方法可以被新的替換。取決于你想做什么。

以上所述是小編給大家介紹的JavaScript 數(shù)組詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • js實現(xiàn)目錄鏈接,內(nèi)容跟著目錄滾動顯示的簡單實例

    js實現(xiàn)目錄鏈接,內(nèi)容跟著目錄滾動顯示的簡單實例

    下面小編就為大家?guī)硪黄猨s實現(xiàn)目錄鏈接,內(nèi)容跟著目錄滾動顯示的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-10-10
  • 面向?qū)ο笤O(shè)計模式的核心法則

    面向?qū)ο笤O(shè)計模式的核心法則

    有本經(jīng)典的書叫《設(shè)計模式》,講了經(jīng)典的21種設(shè)計模式,建議大家都看看
    2013-11-11
  • js百度地圖滾輪縮放所在點偏移問題解決

    js百度地圖滾輪縮放所在點偏移問題解決

    本文主要介紹了js百度地圖滾輪縮放所在點偏移問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • 判斷是否安裝flash player及當(dāng)前版本的JS代碼

    判斷是否安裝flash player及當(dāng)前版本的JS代碼

    本文為大家講述下如何使用jsJS判斷是否安裝flash player及版本,下面的處理代碼或許對大家有所幫助,感興趣的朋友可以參考下,希望對大家有所幫助
    2013-08-08
  • promise結(jié)合requestAnimationFrame用法示例

    promise結(jié)合requestAnimationFrame用法示例

    這篇文章主要為大家介紹了promise結(jié)合requestAnimationFrame用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • 修改js confirm alert 提示框文字的簡單實例

    修改js confirm alert 提示框文字的簡單實例

    下面小編就為大家?guī)硪黄薷膉s confirm alert 提示框文字的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • JavaScript實現(xiàn)代碼雨效果

    JavaScript實現(xiàn)代碼雨效果

    這篇文章主要為大家詳細(xì)介紹了JavaScript實現(xiàn)代碼雨效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • JavaScript數(shù)組push方法使用注意事項

    JavaScript數(shù)組push方法使用注意事項

    push() 方法可向數(shù)組的末尾添加一個或多個元素,并返回新的長度。這篇文章主要介紹了JavaScript數(shù)組push方法使用注意,需要的朋友可以參考下
    2017-10-10
  • JS實現(xiàn)貪吃蛇游戲

    JS實現(xiàn)貪吃蛇游戲

    這篇文章主要介為大家詳細(xì)紹了JS實現(xiàn)貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • 深入學(xué)習(xí)JavaScript執(zhí)行上下文

    深入學(xué)習(xí)JavaScript執(zhí)行上下文

    這篇文章主要介紹了深入學(xué)習(xí)JavaScript執(zhí)行上下文,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下,希望對你的學(xué)習(xí)有所幫助
    2022-08-08

最新評論