15 個必須了解的 Javascript 數(shù)組方法
數(shù)組是任何編程語言的重要組成部分,JavaScript 也不例外。使用數(shù)組,開發(fā)人員可以存儲和操作數(shù)據(jù)集合,包括字符串、數(shù)字甚至對象。
在本文中,我們將介紹每個人都應(yīng)該知道的 15 個必須知道的 JavaScript 數(shù)組方法。
01、Push()
將一個或多個元素添加到數(shù)組末尾 Push() 方法將一個或多個元素添加到數(shù)組末尾并返回數(shù)組的新長度。當(dāng)您需要向數(shù)組添加元素而不指定索引時,此方法非常有用。
const array = [1, 2, 3]; array.push(4, 5); console.log(array); // Output: [1, 2, 3, 4, 5]
02、pop()
刪除并返回數(shù)組中的最后一個元素 pop() 方法刪除并返回數(shù)組中的最后一個元素。當(dāng)您需要從數(shù)組末尾刪除元素時,此方法非常有用。
const array = [1, 2, 3];
const lastElement = array.pop();
console.log(array); // Output: [1, 2]
console.log(lastElement); // Output: 3
03、shift()
刪除并返回數(shù)組中的第一個元素 shift() 方法刪除并返回數(shù)組中的第一個元素。當(dāng)您需要從數(shù)組開頭刪除元素時,此方法非常有用。
const array = [1, 2, 3];
const firstElement = array.shift();
console.log(array); // Output: [2, 3]
console.log(firstElement); // Output: 1
04、unshift()
將一個或多個元素添加到數(shù)組的開頭 unshift() 方法將一個或多個元素添加到數(shù)組的開頭并返回數(shù)組的新長度。當(dāng)您需要在數(shù)組的開頭添加元素時,此方法非常有用。
const array = [1, 2, 3];
array.unshift(4, 5);
console.log(array); // Output: [4, 5, 1, 2, 3]
05、splice()
在數(shù)組的指定索引處添加或刪除元素 splice() 方法在數(shù)組的指定索引處添加或刪除元素。此方法可用于在數(shù)組中的任何位置添加或刪除元素。
const array = [1, 2, 3, 4];
array.splice(1, 2, 5, 6);
console.log(array); // Output: [1, 5, 6, 4]
06、slice()
返回由起始索引和結(jié)束索引指定的數(shù)組部分的副本 slice() 方法返回由起始索引和結(jié)束索引指定的數(shù)組部分的副本。此方法可用于創(chuàng)建包含原始數(shù)組子集的新數(shù)組。
cCopy codeconst array = [1, 2, 3, 4];
const newArray = array.slice(1, 3);
console.log(newArray); // Output: [2, 3]
07、concat()
組合兩個或多個數(shù)組并返回一個新數(shù)組 concat() 方法組合兩個或多個數(shù)組并返回一個新數(shù)組。此方法可用于將數(shù)組連接在一起,而無需修改原始數(shù)組。
arduinoCopy codeconst array1 = [1, 2];
const array2 = [3, 4];
const newArray = array1.concat(array2);
console.log(newArray); // Output: [1, 2, 3, 4]
08、join()
將數(shù)組的所有元素連接成字符串 join() 方法使用指定的分隔符將數(shù)組的所有元素連接成字符串。此方法可用于從數(shù)組創(chuàng)建字符串。
const array = [1, 2, 3]; const string = array.join("-"); console.log(string); // Output: "1-2-3"
09、indexOf()
返回數(shù)組中指定元素第一次出現(xiàn)的索引 indexOf() 方法返回數(shù)組中指定元素第一次出現(xiàn)的索引。如果未找到該元素,則此方法返回 -1。
const array = [1, 2, 3]; const index = array.indexOf(2); console.log(index); // Output: 1
10、lastIndexOf()
返回數(shù)組中最后一次出現(xiàn)的指定元素的索引 lastIndexOf() 方法返回數(shù)組中最后一次出現(xiàn)的指定元素的索引。如果未找到該元素,則此方法返回 -1。
const array = [1, 2, 3, 2]; const index = array.lastIndexOf(2); console.log(index); // Output: 3
11、forEach()
對數(shù)組中的每個元素執(zhí)行一次提供的函數(shù) forEach() 方法對數(shù)組中的每個元素執(zhí)行一次提供的函數(shù)。該方法可用于對數(shù)組的每個元素執(zhí)行操作。
const array = [1, 2, 3]; array.forEach((element) => { console.log(element); }); // Output: // 1 // 2 // 3
12、map()
創(chuàng)建一個新數(shù)組,其中包含對數(shù)組中每個元素調(diào)用提供的函數(shù)的結(jié)果。map() 方法創(chuàng)建一個新數(shù)組,其中包含對數(shù)組中每個元素調(diào)用提供的函數(shù)的結(jié)果。該方法可用于在原始數(shù)組的基礎(chǔ)上創(chuàng)建一個新數(shù)組。
const array = [1, 2, 3]; const newArray = array.map((element) => { return element * 2; }); console.log(newArray); // Output: [2, 4, 6]
13、filter()
創(chuàng)建一個新數(shù)組,其中包含通過所提供函數(shù)指定的測試的所有元素。filter() 方法創(chuàng)建一個新數(shù)組,其中包含通過所提供函數(shù)指定的測試的所有元素。此方法可用于根據(jù)條件創(chuàng)建新數(shù)組。
const array = [1, 2, 3]; const newArray = array.filter((element) => { return element > 1; }); console.log(newArray); // Output: [2, 3]
14、reduce()
對數(shù)組的每個元素執(zhí)行提供的函數(shù)并返回單個值。reduce() 方法對數(shù)組的每個元素執(zhí)行提供的函數(shù)并返回單個值。此方法可用于對數(shù)組的所有元素執(zhí)行操作并返回單個值。
const array = [1, 2, 3]; const sum = array.reduce((accumulator, currentValue) => { return accumulator + currentValue; }, 0); console.log(sum); // Output: 6
15、sort()
對數(shù)組的元素就地排序 sort() 方法對數(shù)組的元素就地排序。此方法可用于按升序或降序?qū)?shù)組進(jìn)行排序。
const array = [3, 2, 1];
array.sort();
console.log(array); // Output: [1, 2, 3]
結(jié)論
在今天的文章中,我們介紹了15 個開發(fā)者必須知道的 JavaScript 數(shù)組方法。這些方法對于在 JavaScript 中使用數(shù)組至關(guān)重要,并且可以極大地簡化您的代碼。
通過使用這些方法,您可以對數(shù)組執(zhí)行各種操作,例如添加、刪除、排序和過濾元素。無論您是初學(xué)者還是經(jīng)驗(yàn)豐富的開發(fā)人員,掌握這些數(shù)組方法都將使您的生活更輕松,代碼更高效。