JavaScript中Array的常用方法舉例詳解
前言
JavaScript 中的 Array
對(duì)象提供了許多常用的方法,這些方法可以幫助你更方便地操作數(shù)組。以下是一些常用的 Array
方法及其用法:
1. 創(chuàng)建和初始化數(shù)組
new Array()
:創(chuàng)建一個(gè)空數(shù)組。Array.of()
:創(chuàng)建一個(gè)具有可變數(shù)量參數(shù)的新數(shù)組實(shí)例。Array.from()
:從類數(shù)組或可迭代對(duì)象創(chuàng)建一個(gè)新的數(shù)組實(shí)例。
const arr1 = new Array(); const arr2 = Array.of(1, 2, 3); const arr3 = Array.from('hello'); // ['h', 'e', 'l', 'l', 'o']
2. 添加和刪除元素
push(...items)
:在數(shù)組末尾添加一個(gè)或多個(gè)元素,并返回新的長度。pop()
:移除數(shù)組末尾的元素,并返回該元素。unshift(...items)
:在數(shù)組開頭添加一個(gè)或多個(gè)元素,并返回新的長度。shift()
:移除數(shù)組開頭的元素,并返回該元素。
const arr = [1, 2, 3]; arr.push(4); // arr 現(xiàn)在是 [1, 2, 3, 4] console.log(arr.pop()); // 輸出: 4, arr 現(xiàn)在是 [1, 2, 3] arr.unshift(0); // arr 現(xiàn)在是 [0, 1, 2, 3] console.log(arr.shift()); // 輸出: 0, arr 現(xiàn)在是 [1, 2, 3]
3. 查找元素
indexOf(searchElement[, fromIndex])
:返回第一個(gè)匹配項(xiàng)的索引,如果沒有找到則返回 -1。lastIndexOf(searchElement[, fromIndex])
:返回最后一個(gè)匹配項(xiàng)的索引,如果沒有找到則返回 -1。find(callback[, thisArg])
:返回?cái)?shù)組中滿足提供的測試函數(shù)的第一個(gè)元素的值,如果沒有找到則返回undefined
。findIndex(callback[, thisArg])
:返回?cái)?shù)組中滿足提供的測試函數(shù)的第一個(gè)元素的索引,如果沒有找到則返回 -1。
const arr = [1, 2, 3, 4, 5]; console.log(arr.indexOf(3)); // 輸出: 2 console.log(arr.lastIndexOf(3)); // 輸出: 2 const foundElement = arr.find(element => element > 3); console.log(foundElement); // 輸出: 4 const foundIndex = arr.findIndex(element => element > 3); console.log(foundIndex); // 輸出: 3
4. 遍歷數(shù)組
forEach(callback[, thisArg])
:對(duì)數(shù)組中的每個(gè)元素執(zhí)行一次提供的函數(shù)。map(callback[, thisArg])
:創(chuàng)建一個(gè)新數(shù)組,其結(jié)果是該數(shù)組中的每個(gè)元素調(diào)用提供的函數(shù)的結(jié)果。filter(callback[, thisArg])
:創(chuàng)建一個(gè)新數(shù)組,包含通過提供的函數(shù)實(shí)現(xiàn)的測試的所有元素。reduce(callback[, initialValue])
:對(duì)數(shù)組中的每個(gè)元素執(zhí)行一個(gè)由您提供的 reducer 函數(shù)(升序執(zhí)行),將其結(jié)果匯總為單個(gè)返回值。reduceRight(callback[, initialValue])
:與reduce
類似,但降序執(zhí)行。
const arr = [1, 2, 3, 4, 5]; // forEach arr.forEach(element => console.log(element)); // map const squared = arr.map(element => element * element); console.log(squared); // 輸出: [1, 4, 9, 16, 25] // filter const evenNumbers = arr.filter(element => element % 2 === 0); console.log(evenNumbers); // 輸出: [2, 4] // reduce const sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0); console.log(sum); // 輸出: 15 // reduceRight const reversedSum = arr.reduceRight((accumulator, currentValue) => accumulator + currentValue, 0); console.log(reversedSum); // 輸出: 15
5. 數(shù)組轉(zhuǎn)換
slice([begin[, end]])
:返回一個(gè)新的數(shù)組,包含從開始到結(jié)束(不包括結(jié)束)的數(shù)組的一部分淺拷貝。splice(start[, deleteCount[, ...items]])
:改變?cè)瓟?shù)組,通過刪除現(xiàn)有元素和/或添加新元素。concat(...items)
:返回一個(gè)新的數(shù)組實(shí)例,該實(shí)例是通過將現(xiàn)有數(shù)組與傳入的數(shù)組或非數(shù)組值連接而成的。flat(depth)
:按照指定深度遞歸地展平數(shù)組。flatMap(callback[, thisArg])
:先映射數(shù)組,然后將結(jié)果展平。
const arr = [1, 2, 3, 4, 5]; // slice const sliced = arr.slice(1, 3); console.log(sliced); // 輸出: [2, 3] // splice const removed = arr.splice(1, 2, 10, 11); console.log(arr); // 輸出: [1, 10, 11, 4, 5] console.log(removed); // 輸出: [2, 3] // concat const concatenated = arr.concat([6, 7]); console.log(concatenated); // 輸出: [1, 10, 11, 4, 5, 6, 7] // flat const nested = [1, [2, [3, [4, 5]]]]; const flattened = nested.flat(Infinity); console.log(flattened); // 輸出: [1, 2, 3, 4, 5] // flatMap const mappedAndFlattened = arr.flatMap(x => [x, x * 2]); console.log(mappedAndFlattened); // 輸出: [1, 2, 10, 20, 11, 22, 4, 8, 5, 10]
6. 排序和反轉(zhuǎn)
sort([compareFunction])
:對(duì)數(shù)組的元素進(jìn)行排序,并返回排序后的數(shù)組。reverse()
:反轉(zhuǎn)數(shù)組中元素的順序,并返回反轉(zhuǎn)后的數(shù)組。
const arr = [3, 1, 4, 1, 5, 9, 2, 6]; // sort arr.sort((a, b) => a - b); console.log(arr); // 輸出: [1, 1, 2, 3, 4, 5, 6, 9] // reverse arr.reverse(); console.log(arr); // 輸出: [9, 6, 5, 4, 3, 2, 1, 1]
7. 其他方法
includes(searchElement[, fromIndex])
:判斷數(shù)組是否包含某個(gè)指定的值,如果是返回true
,否則返回false
。join([separator])
:將所有數(shù)組元素連接成一個(gè)字符串。some(callback[, thisArg])
:檢測數(shù)組中是否有至少一個(gè)元素滿足提供的測試函數(shù)。every(callback[, thisArg])
:檢測數(shù)組中的所有元素是否都滿足提供的測試函數(shù)。
const arr = [1, 2, 3, 4, 5]; // includes console.log(arr.includes(3)); // 輸出: true // join const joined = arr.join('-'); console.log(joined); // 輸出: 1-2-3-4-5 // some const hasEven = arr.some(element => element % 2 === 0); console.log(hasEven); // 輸出: true // every const allPositive = arr.every(element => element > 0); console.log(allPositive); // 輸出: true
總結(jié)
到此這篇關(guān)于JavaScript中Array常用方法的文章就介紹到這了,更多相關(guān)JS中Array常用方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
微信小程序內(nèi)拖動(dòng)圖片實(shí)現(xiàn)移動(dòng)、放大、旋轉(zhuǎn)的方法
這篇文章主要介紹了微信小程序內(nèi)拖動(dòng)圖片實(shí)現(xiàn)移動(dòng)、放大、旋轉(zhuǎn)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09JavaScript實(shí)現(xiàn)表格快速變色效果代碼
這篇文章主要介紹了JavaScript實(shí)現(xiàn)表格快速變色效果的方法,通過javascript數(shù)組遍歷結(jié)合時(shí)間函數(shù)來實(shí)現(xiàn)表格快速變色的功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08超出JavaScript安全整數(shù)限制的數(shù)字計(jì)算BigInt詳解
這篇文章給大家分享了超出JavaScript安全整數(shù)限制的數(shù)字計(jì)算BigInt的相關(guān)知識(shí)點(diǎn),有興趣的朋友參考學(xué)習(xí)下。2018-06-06普通web整合quartz跑定時(shí)任務(wù)的示例
這篇文章主要介紹了普通web整合quartz跑定時(shí)任務(wù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03JavaScript顯示當(dāng)然日期和時(shí)間即年月日星期和時(shí)間
使用js顯示當(dāng)然日期和時(shí)間在網(wǎng)頁中很是常見,方法有很多,不過多說大同小異,下面有個(gè)不錯(cuò)的示例,需要的朋友可以感受下2013-10-10Boostrap柵格系統(tǒng)與自己額外定義的媒體查詢的沖突問題
這篇文章主要介紹了Boostrap柵格系統(tǒng)與自己額外定義的媒體查詢的沖突問題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02微信小程序swiper左右擴(kuò)展各顯示一半代碼實(shí)例
這篇文章主要介紹了微信小程序swiper左右擴(kuò)展各顯示一半代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12