ECMAScript學習二:Array數組方法

1、Map集合

1.1、格式:

var new_array =  arr.map(function(currentValue[, index[, array]]) {
          // Return element for new_array
}

1.2、返回值說明

The map() method creates a new array with the results of calling a provided function on every element in the calling array.
該方法會返回一個同原數組長度一樣的一個新數組,數組中的每一項,就是函數調用後的結果

  • 注意: map() 不會對空數組進行檢測。
  • 注意:map() 不會改變原始數組。

1.3、使用

let arr = [1, 2, 3, 4, 5, 6, 7];
let a = arr.map((item, index,arr) => {
   console.log(arr);//(7) [1, 2, 3, 4, 5, 6, 7]
   return item * 2;
});
console.log(a);//(7) [2, 4, 6, 8, 10, 12, 14]

2、filter函數

2.1、形式

var newArray = arr.filter(function(element[, index[, array]])
[, thisArg])

2.2、返回值說明:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.
filter() 方法創建一個新的數組,新數組中的元素是通過檢查指定數組中符合條件的所有元素。
注意: filter() 不會對空數組進行檢測。
注意: filter() 不會改變原始數組。

2.3、實例

let arr = [1, 2, 3, 4, 5, 6, 7];
let a = arr.filter((item, index, arr) => {
   return item > 3;
});
console.log(a);//(4) [4, 5, 6, 7]

3、arr.every

3.1、定義

arr.every(function(element[, index[, array]])[, thisArg])

3.2、返回值說明

The every() method tests whether all elements in the array pass the test implemented by the provided function.
數組中的每一項運行該函數,如果每一項的函數都返回true,則該方法返回真。
every() 方法用於檢測數組所有元素是否都符合指定條件(通過函數提供)。
every() 方法使用指定函數檢測數組中的所有元素:
(1)如果數組中檢測到有一個元素不滿足,則整個表達式返回 false ,且剩餘的元素不會再進行檢測。
(2)如果所有元素都滿足條件,則返回 true。

  • 注意: every() 不會對空數組進行檢測。
  • 注意: every() 不會改變原始數組。

3.3、實例

let arr = [1, 2, 3, 4, 5, 6, 7];
let a = arr.every(function(item) {
   return item > 1;
});
console.log(a);//false

4、arr.some()

4.1、定義

arr.some(callback[, thisArg])

4.2、返回值

數組中的每一項,運行給定函數,只要有一項返回真,那麼就返回真。

  • 注意: some() 不會對空數組進行檢測。
  • 注意: some() 不會改變原始數組。

4.3、實例

let arr = [1, 2, 3, 4, 5, 6, 7];
let a = arr.some(item => item > 6);
console.log(a);//true

5、arr.reduce()

歸併函數

5.1、定義

arr.reduce(function[, initialValue])

5.2、返回值說明

reduce() 方法接收一個函數作爲累加器,數組中的每個值(從左到右)開始縮減,最終計算爲一個值。

  • reduce() 可以作爲一個高階函數,用於函數的 compose。
  • 注意: reduce() 對於空數組是不會執行回調函數的。

5.3、實例

let arr = [1, 2, 3, 4, 5];
let a = arr.reduce(function(prev, item, index, arr) {
  return prev + item;
});
console.log('最終結果是:' + a);//最終結果是:15

let a = arr.reduce(function(prev, item, index, arr) {
    return prev + item;
}, 10);
console.log('最終結果是:' + a);//最終結果是:25

6、arr.filter()

6.1、定義

var newArray = arr.filter(function(element[, index[, array]])[, thisArg])

6.2、返回值說明

filter() 方法創建一個新的數組,新數組中的元素是通過檢查指定數組中符合條件的所有元素。
注意: filter() 不會對空數組進行檢測。
注意: filter() 不會改變原始數組。

6.3、實例

數組去重

let arr = [2, 2, 4, 2, 1, 4, 2, 2, 2, 24, 4];
let a = arr.filter((item, index, arr) => arr.indexOf(item) === index);
console.log(a);//(4) [2, 4, 1, 24]

7、arr.sort(sortby)

方法用於對數組的元素進行排序。

  • (1)如果調用該方法時沒有使用參數,將按字母順序對數組中的元素進行排序,說得更精確點,是按照字符編碼的順序進行排序。要實現這一點,首先應把數組的元素都轉換成字符串(如有必要),以便進行比較。

  • (2)如果想按照其他標準進行排序,就需要提供比較函數,該函數要比較兩個值,然後返回一個用於說明這兩個值的相對順序的數字。比較函數應該具有兩個參數 a 和 b,其返回值如下:

  • <1>:若 a 小於 b,在排序後的數組中 a 應該出現在 b 之前,則返回一個小於 0 的值。

  • <2>:若 a 等於 b,則返回 0。

  • <3>:若 a 大於 b,則返回一個大於 0 的值。

arr.sort((a,b)=>{return a-b}

let arr = [12,14,3,45,56,78,123,1,2,5]
let result = arr.sort((a,b)=>{return a-b})
console.log(result) //(10) [1, 2, 3, 5, 12, 14, 45, 56, 78, 123]

8、forEach循環遍歷數組

8.1、形式:

arr.forEach(function (item, index, arr){});

8.2、作用:

遍歷數組

8.3、參數說明

(1) 可以接收兩個參數,第一個參數是一個函數,就是數組的每一項要運行這個函數。這個函數接收三個參數,分別是:數組中的項,下標,數組本身
(2)第二個參數是一個數組(可有可無)。如果有,前面函數中的this就指向這個數組;如果沒有,前面函數的this就指向window。

let arr = [1, 2, 3, 4, 5, 6, 7];
arr.forEach(function(item, index) {
    console.log(item+"============"+index);
    //1============0
    console.log(this);
    //(7) [1, 2, 3, 4, 5, 6, 7]
},arr);

9、for-of循環遍歷數組

let a = ['A', 'B', 'C'];
for (let attr of a) {
   console.log(attr);//A B C
}

10、Array.from()

方法從一個類似數組或可迭代對象創建一個新的,淺拷貝的數組實例。
Array.from()方法就是將一個類數組對象或者可遍歷對象轉換成一個真正的數組。那麼什麼是類數組對象呢?所謂類數組對象,最基本的要求就是具有length屬性的對象。

console.log(Array.from('foo'));
// expected output: Array ["f", "o", "o"]

console.log(Array.from([1, 2, 3], x => x + x));
// expected output: Array [2, 4, 6]

let arr2 = [1,1,2,2,3,3,4,5,75,56]
// let array = new Set(arr2) //(7) [1, 2, 3, 4, 5, 75, 56]
let array = Array.from(new Set(arr2))
console.log(array) //(7) [1, 2, 3, 4, 5, 75, 56]

let ele = document.getElementsByTagName("input")
    console.log(ele) // HTMLCollection(8) [input, input, input, input, input, input, input, input, dept: input, all: input]
    let array = Array.from(ele)
    console.log(array) // (8)[input, input, input, input, input, input, input, input]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章