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