JavaScript中Array的常用方法舉例詳解
前言
JavaScript 中的 Array 對象提供了許多常用的方法,這些方法可以幫助你更方便地操作數(shù)組。以下是一些常用的 Array 方法及其用法:
1. 創(chuàng)建和初始化數(shù)組
new Array():創(chuàng)建一個(gè)空數(shù)組。Array.of():創(chuàng)建一個(gè)具有可變數(shù)量參數(shù)的新數(shù)組實(shí)例。Array.from():從類數(shù)組或可迭代對象創(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]):對數(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]):對數(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]]):改變原數(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]):對數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
layui點(diǎn)擊左側(cè)導(dǎo)航欄,實(shí)現(xiàn)不刷新整個(gè)頁面,只刷新局部的方法
今天小編就為大家分享一篇layui點(diǎn)擊左側(cè)導(dǎo)航欄,實(shí)現(xiàn)不刷新整個(gè)頁面,只刷新局部的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
js購物車實(shí)現(xiàn)思路及代碼(個(gè)人感覺不錯(cuò))
提起購物車想必只有在一些購物網(wǎng)站上才可以看得到,下面為大家介紹下使用js實(shí)現(xiàn)的購物車,感興趣的朋友可以參考下2013-12-12
js實(shí)現(xiàn)頁面打印功能實(shí)例代碼(附去頁眉頁腳功能代碼)
js實(shí)現(xiàn)頁面打印功能實(shí)例代碼(附去頁眉頁腳功能代碼)2009-12-12
使用js實(shí)現(xiàn)一個(gè)簡單的滾動條過程解析
這篇文章主要介紹了使用js實(shí)現(xiàn)一個(gè)簡單的滾動條過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
詳解JavaScript對Date對象的操作問題(生成一個(gè)倒數(shù)7天的數(shù)組)
最近項(xiàng)目需求要生成一個(gè)倒數(shù)7天的數(shù)組,下面小編把我的實(shí)現(xiàn)思路和代碼整理分享給大家,供大家參考,需要的朋友可以參考下2015-10-10

