修改原數組 | 不修改原數組 |
---|---|
push, pop | concat |
unshift,shift | join |
sort | slice |
reverse | indexOf(),lastIndexOf() |
splice | forEach |
copyWithin | map |
fill | filter |
some | |
every | |
reduce,reduceRight | |
includes | |
finde,findIndex | |
entries(),keys(),values() |
arrayObject.splice(index,howmany,item1,.....,itemX)數組
var arr = new Array(); arr[0] = "George"; arr[1] = "John"; arr[2] = "Thomas"; arr.splice(2,1); //"George", "John" arr.splice(1,0,"William"); //"George", "William", "John" arr.splice(2,1,"haha"); //"George", "William", "haha"
arrayObject.slice(start,end);瀏覽器
var arr = new Array(); arr[0] = "George"; arr[1] = "John"; arr[2] = "Thomas"; arr.slice(2,1); //[] arr.slice(1,2); //"William" arr.slice(-2,-1); //"William"
將相似數組的對象(好比arguments)轉換爲真實的數組
Array.prototype.slice.call(arguments);
方法並不會修改原數組
array.indexOf(searchElement[, fromIndex])
var data = [2, 5, 7, 3, 5]; console.log(data.indexOf(5, "x")); // 1 ("x"被忽略) console.log(data.indexOf(5, "3")); // 4 (從3號位開始搜索)
array.lastIndexOf(searchElement[, fromIndex])
var data = [2, 5, 7, 3, 5]; console.log(data.lastIndexOf(5)); // 4 console.log(data.lastIndexOf(5, 3)); // 1 (從後往前,索引值小於3的開始搜索) console.log(data.lastIndexOf(4)); // -1 (未找到)
兩個方法在比較第一個參數與數組中的每一項時,會使用全等操做符, 要求必須徹底相等,不然返回-1。
每一個方法都接受兩個參數,第一個參數callback(回調函數,必選),第二個參數是一個可選的上下文參數。數據結構
須要注意的是除了forEach()方法,其他的迭代方法中callback須要有return值不然會返回undefined。wordpress
forEach()對數組進行遍歷循環,對數組中的每一項運行給定的函數,這個方法沒有返回值。函數
[].forEach(function(value, index, array) { // ... }, [ thisObject]);
[1, 2, 3, 4].forEach(function (item, index, array) { console.log(array[index] == item); // true sum += item; }); alert(sum); // 10 var database = { users: ["張含韻", "江一燕", "李小璐"], sendEmail: function (user) { if (this.isValidUser(user)) { console.log("你好," + user); } else { console.log("抱歉,"+ user +",你不是本家人"); } }, isValidUser: function (user) { return /^張/.test(user); } }; // 給每一個人法郵件 database.users.forEach( // database.users中人遍歷 database.sendEmail, // 發送郵件 database // 使用database代替上面標紅的this ); // 結果: // 你好,張含韻 // 抱歉,江一燕,你不是本家人 // 抱歉,李小璐,你不是本家
map()指「映射」,對數組中的每一項運行給定的函數,返回每次函數調用的結果組成的數組。this
[].map(function(value, index, array) { // ... }, [ thisObject]);
var data = [1, 2, 3, 4]; var arrayOfSquares = data.map(function (item) { return item * item; }); alert(arrayOfSquares); // 1, 4, 9, 16
filter(),「過濾」,對數組中的每一項運行給定函數,返回知足過濾條件組成的數組。es5
array.filter(callback,[ thisObject]);
var arr3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var flag = arr3.filter(function(value, index) { return value % 3 === 0; }); console.log(flag); // [3, 6, 9]
every(),判斷數組中每一項都是否知足所給條件,當全部項都知足條件,纔會返回true。prototype
array.every(callback,[ thisObject]);
var arr4 = [1, 2, 3, 4, 5]; var flag = arr4.every(function(value, index) { return value % 2 === 0; }); console.log(flag); // false
some(),判斷數組中是否存在知足條件的項,只要有一項知足條件,就會返回true。code
array.some(callback,[ thisObject]);
var arr5 = [1, 2, 3, 4, 5]; var flag = arr5.some(function(value, index) { return value % 2 === 0; }); console.log(flag); // true
這兩個方法相比前面可能稍微複雜一些,它們都會迭代數組中的全部項,而後生成一個最終返回值。這兩個方法接收兩個參數。對象
reduce() 方法接收一個函數做爲累加器,數組中的每一個值(從左到右)開始縮減,最終計算爲一個值。 reduce() 對於空數組是不會執行回調函數的。
array. reduce(function(total, currentValue, currentIndex, array) { // ... });
var arr9 = [1, 2, 3, 4]; var sum9 =arr9.reduce(function (total, curr, index, array) { return total * curr; }); console.log(sum9); // 24 var sum9_1 =arr9.reduce(function (total, curr, index, array) { return total * curr; }, 10); console.log(sum9_1); // 240
reduceRight()和reduce()相比,用法相似,差別在於reduceRight是從數組的末尾開始實現的。
array.reduceRight(callback,[ thisObject]);
var arr9 = [2, 45, 30, 80]; var flag = arr9.reduceRight(function (total, curr, index) { return total - curr; }); var flag_1 = arr9.reduceRight(function (total, curr, index) { return total - curr; },200); console.log(flag); // 3 console.log(flag_1); // 43
判斷參數是不是」Array」返回true或false。
var a = [1,2,3]; Array.isArray(a); //true
用於將兩類對象轉爲真正的數組:相似數組的對象(array-like object)和可遍歷(iterable)的對象(包括 ES6 新增的數據結構 Set 和 Map)。
let arrayLike = { '0': 'a', '1': 'b', '2': 'c', length: 3 }; let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
Array.from還能夠接受第二個參數,做用相似於數組的map方法,用來對每一個元素進行處理,將處理後的值放入返回的數組。
Array.from(arrayLike, x => x * x); // 等同於 Array.from(arrayLike).map(x => x * x); Array.from([1, 2, 3], (x) => x * x) // [1, 4, 9]
用於將一組值,轉換爲數組。
Array.of(3, 11, 8) // [3,11,8] Array.of(3) // [3]
在當前數組內部,將指定位置的成員複製到其餘位置(會覆蓋原有成員),而後返回當前數組。
array. copyWithin(target, start = 0, end = this.length);
// 將3號位複製到0號位 [1, 2, 3, 4, 5].copyWithin(0, 3, 4) // [4, 2, 3, 4, 5] // -2至關於3號位,-1至關於4號位 [1, 2, 3, 4, 5].copyWithin(0, -2, -1) // [4, 2, 3, 4, 5]
使用給定值,填充一個數組。
array. copyWithin(target, start = 0, end = this.length);
['a', 'b', 'c'].fill(7); // [7, 7, 7] let arr = new Array(3).fill([]); arr[0].push(5); // [[5], [5], [5]]
用於找出第一個符合條件的數組成員。它的參數是一個回調函數,全部數組成員依次執行該回調函數,直到找出第一個返回值爲true的成員,而後返回該成員。若是沒有符合條件的成員,則返回undefined。
find方法的回調函數能夠接受三個參數,依次爲當前的值、當前的位置和原數組。
array.reduceRight(callback,[ thisObject]);
[1, 4, -5, 10].find((n) => n < 0) // -5 [1, 5, 10, 15].find(function(value, index, arr) { return value > 9; }) // 10
findIndex方法的用法與find方法很是相似,返回第一個符合條件的數組成員的位置,若是全部成員都不符合條件,則返回-1。
array.reduceRight(callback,[ thisObject]);
[1, 5, 10, 15].findIndex(function(value, index, arr) { return value > 9; }) // 2
返回一個布爾值,表示某個數組是否包含給定的值。
[1, 2, 3].includes(2) // true
ES6提供了三個新方法:entries()、keys()和values(),用來遍歷數組。它們都返回一個遍歷器對象,能夠用for...of循環進行遍歷,惟一的區別是keys()是對數組的鍵名的遍歷、values()是對數組鍵值的遍歷,entries()方法是對數值的鍵值對的遍歷。
for (let index of ['a', 'b'].keys()) { console.log(index); } // 0 // 1 for (let elem of ['a', 'b'].values()) { console.log(elem); } // 'a' // 'b' for (let [index, elem] of ['a', 'b'].entries()) { console.log(index, elem); } // 0 "a" // 1 "b"
若是不使用for...of循環,能夠手動調用遍歷器對象的next方法,進行遍歷。
let letter = ['a', 'b', 'c']; let entries = letter.entries(); console.log(entries.next().value); // [0, 'a'] console.log(entries.next().value); // [1, 'b'] console.log(entries.next().value); // [2, 'c']