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