1. 程式人生 > 實用技巧 >【JavaScript】Array 例項方法(三)

【JavaScript】Array 例項方法(三)

以下內容為學習記錄,可以參考 MDN 原文。

環境

  • node v12.18.1
  • npm 6.14.5
  • vscode 1.46
  • Microsoft Edge 83

reduceRight

reduceRight() 方法接受一個函式作為累加器(accumulator)和陣列的每個值(從右到左)將其減少為單個值。

const array1 = [[0, 1], [2, 3], [4, 5]].reduceRight(
  (accumulator, currentValue) => accumulator.concat(currentValue)
);

console.log(array1);
// expected output: Array [4, 5, 2, 3, 0, 1]

reverse

reverse() 方法將陣列中元素的位置顛倒,並返回該陣列。陣列的第一個元素會變成最後一個,陣列的最後一個元素變成第一個。該方法會改變原陣列。

const array1 = ['one', 'two', 'three'];
console.log('array1:', array1);
// expected output: "array1:" Array ["one", "two", "three"]

const reversed = array1.reverse();
console.log('reversed:', reversed);
// expected output: "reversed:" Array ["three", "two", "one"]

// Careful: reverse is destructive -- it changes the original array.
console.log('array1:', array1);
// expected output: "array1:" Array ["three", "two", "one"]

shift

shift() 方法從陣列中刪除第一個元素,並返回該元素的值。此方法更改陣列的長度。

const array1 = [1, 2, 3];

const firstElement = array1.shift();

console.log(array1);
// expected output: Array [2, 3]

console.log(firstElement);
// expected output: 1

slice

slice() 方法返回一個新的陣列物件,這一物件是一個由 begin 和 end 決定的原陣列的淺拷貝(包括 begin,不包括 end)。原始陣列不會被改變。

const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]

some

some() 方法測試陣列中是不是至少有 1 個元素通過了被提供的函式測試。它返回的是一個 Boolean 型別的值。

const array = [1, 2, 3, 4, 5];

// checks whether an element is even
const even = (element) => element % 2 === 0;

console.log(array.some(even));
// expected output: true

sort

sort() 方法用原地演算法對陣列的元素進行排序,並返回陣列。預設排序順序是在將元素轉換為字串,然後比較它們的 UTF-16 程式碼單元值序列時構建的

const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]

const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// expected output: Array [1, 100000, 21, 30, 4]

splice

splice() 方法通過刪除或替換現有元素或者原地新增新的元素來修改陣列,並以陣列形式返回被修改的內容。此方法會改變原陣列。

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]

toString

toString() 返回一個字串,表示指定的陣列及其元素。

const array1 = [1, 2, 'a', '1a'];

console.log(array1.toString());
// expected output: "1,2,a,1a"

unshift

unshift() 方法將一個或多個元素新增到陣列的開頭,並返回該陣列的新長度(該方法修改原有陣列)。

const array1 = [1, 2, 3];

console.log(array1.unshift(4, 5));
// expected output: 5

console.log(array1);
// expected output: Array [4, 5, 1, 2, 3]

values

values() 方法返回一個新的 Array Iterator 物件,該物件包含陣列每個索引的值

const array1 = ['a', 'b', 'c'];
const iterator = array1.values();

for (const value of iterator) {
  console.log(value);
}

// expected output: "a"
// expected output: "b"
// expected output: "c"