數(shù)組,是JavaScript中的一種數(shù)據(jù)格式,在JavaScript中經(jīng)常使用。作為一名前端工程師,掌握Array的用法非常重要!
forEach() 方法通常用于對(duì)數(shù)組中的每個(gè)元素執(zhí)行操作,而不返回新數(shù)組。它提供了一種迭代數(shù)組并對(duì)每個(gè)元素執(zhí)行相同操作的便捷方法。
注意:forEach()方法不能中斷或跳過(guò)迭代,它會(huì)遍歷數(shù)組中的每個(gè)元素,即使回調(diào)函數(shù)中使用了return語(yǔ)句,也不會(huì)中止遍歷。
功能:對(duì)數(shù)組中的每個(gè)元素執(zhí)行指定的函數(shù),并返回由執(zhí)行結(jié)果組成的新數(shù)組。
用法示例:
map()方法可以根據(jù)自定義的處理邏輯對(duì)數(shù)組中的每個(gè)元素進(jìn)行變換。您可以使用它生成一個(gè)新數(shù)組,其元素是處理原始數(shù)組的結(jié)果。
常見(jiàn)的使用場(chǎng)景包括對(duì)數(shù)組中每個(gè)元素的計(jì)算、轉(zhuǎn)換、映射等操作。
作用:根據(jù)指定條件過(guò)濾掉數(shù)組中符合條件的元素,返回一個(gè)新數(shù)組
用法示例:
使用filter方法,可以從數(shù)組中過(guò)濾掉滿足特定條件的元素。
功能:對(duì)數(shù)組中的所有元素執(zhí)行指定的歸約函數(shù),并返回單值結(jié)果
參數(shù)說(shuō)明:callback是回調(diào)函數(shù),可以接受四個(gè)參數(shù):
用法示例:
調(diào)用reduce()方法,傳入累加函數(shù)(accumulator, currentValue) => Accumulator + currentValue,累加數(shù)組中所有元素。
累加器的初始值未指定,因此,reduce() 方法從數(shù)組的第一個(gè)元素開(kāi)始,將當(dāng)前元素添加到累加器,并更新累加器的值。最后返回的累加結(jié)果是數(shù)組所有元素的累加和。
功能:對(duì)數(shù)組元素進(jìn)行排序
用法示例:
// sort()const fruits = ['banana','apple','kiwi','pear'];fruits.sort();console.log(fruits);// Output:['apple','banana','kiwi','pear']
1、不傳遞參數(shù)調(diào)用sort,會(huì)直接修改原數(shù)組,返回排序后的數(shù)組;
2、傳入比較函數(shù),該函數(shù)接受兩個(gè)參數(shù),返回一個(gè)代表比較結(jié)果的數(shù)字;
功能:反轉(zhuǎn)數(shù)組中元素的順序
通過(guò)使用reverse()方法,可以輕松反轉(zhuǎn)數(shù)組中元素的順序,適用于需要反轉(zhuǎn)數(shù)組內(nèi)容的情況。
注意:reverse()方法會(huì)直接修改原數(shù)組,不會(huì)創(chuàng)建新數(shù)組。如果需要保留原始數(shù)組的副本并執(zhí)行反向操作,可以先使用 slice() 方法創(chuàng)建一個(gè)新數(shù)組,然后調(diào)用reverse() 方法。
功能:判斷數(shù)組是否包含指定元素,返回true或false
用法示例:
1. 檢查數(shù)組是否包含特定元素:
const numbers = [1, 2, 3, 4, 5];console.log(numbers.includes(3)); // trueconsole.log(numbers.includes(6)); // false
2.使用fromIndex參數(shù)指定搜索的起始位置:
const numbers = [1, 2, 3, 4, 5];console.log(numbers.includes(3, 2)); // true, search starts from index 2console.log(numbers.includes(3, 4)); // false, search starts from index 4
3. 檢查數(shù)組中是否包含字符串:
const fruits = ['apple', 'banana', 'kiwi', 'pear'];console.log(fruits.includes('banana')); // trueconsole.log(fruits.includes('grape')); // false
功能:檢查數(shù)組中是否至少有一個(gè)元素滿足指定條件,返回true或false
用法示例:
1、檢查數(shù)組中是否有大于10的元素:
const numbers = [5, 8, 12, 3, 9];const hasGreaterThan10 = numbers.some(element => element > 10);console.log(hasGreaterThan10); // true
2. 檢查數(shù)組中是否有偶數(shù):
const numbers = [3, 7, 5, 12, 9];const hasEvenNumber = numbers.some(element => element % 2 === 0);console.log(hasEvenNumber); // true
3. 檢查數(shù)組中是否存在包含特定字符的字符串:
const fruits = ['apple', 'banana', 'kiwi', 'pear'];const hasStrWithChar = fruits.some(element => element.includes('a'));console.log(hasStrWithChar); // true
4、檢查數(shù)組中是否存在滿足復(fù)雜條件的元素:
const students = [ { name: 'Alice', score: 85 }, { name: 'Bob', score: 92 }, { name: 'Charlie', score: 76 },];const hasPassingScore = students.some(student => student.score >= 80);console.log(hasPassingScore); // true
功能:檢查數(shù)組中所有元素是否滿足指定條件,返回true或false
用法示例:
1.檢查數(shù)組中的所有元素是否都大于 0:
const numbers = [5, 8, 12, 3, 9];const allGreaterThan0 = numbers.every(element => element > 0);console.log(allGreaterThan0); // true
2.檢查數(shù)組中的所有元素是否都是偶數(shù):
const numbers = [2, 4, 6, 8, 10];const allEvenNumbers = numbers.every(element => element % 2 === 0);console.log(allEvenNumbers); // true
3.檢查數(shù)組中的所有字符串是否以大寫字母開(kāi)頭:
const words = ['Apple', 'Banana', 'Cherry', 'Durian'];const allUpperCaseStart = words.every(element => /^[A-Z]/.test(element));console.log(allUpperCaseStart); // true
4.檢查數(shù)組中的所有對(duì)象是否滿足特定條件:
const students = [ { name: 'Alice', score: 85 }, { name: 'Bob', score: 92 }, { name: 'Charlie', score: 76 },];const allPassingScore = students.every(student => student.score >= 80);console.log(allPassingScore); // false
以上就是我今天與你分享的19個(gè)常用的Array方法, 你學(xué)會(huì)了嗎?
當(dāng)然,Array在ES6中還有一些更高級(jí)的使用方法,可以更加快速地操作Array。
本文鏈接:http://www.tebozhan.com/showinfo-26-10493-0.html19個(gè)JavaScript數(shù)組常用方法總結(jié)! 趕快收藏吧!
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com