1、Array.from()----->可將一個有length屬性的字符串或者對象轉換成數組的形式(Array.from
方法用於將兩類對象轉爲真正的數組:類似數組的對象(array-like
object)和可遍歷(iterable)的對象(包括ES6新增的數據結構Set和Map)。)
可以傳入三個參數,Array.from([ ],function,this):第一個參數是含有length屬性的對象或者元素,第二個是類似數組的map方法可以遍歷數組中的每一個元素,最後一個參數是綁定his的指向的(如果map
函數裏面用到了this
關鍵字,還可以傳入Array.from
的第三個參數,用來綁定this
。)
Array.from()
的另一個應用是,將字符串轉爲數組,然後返回字符串的長度。因爲它能正確處理各種Unicode字符,可以避免JavaScript將大於\uFFFF
的Unicode字符,算作兩個字符的bug。
function countSymbols(string) {
return Array.from(string).length;
}
Array.of()
Array.of()------>主要是將輸入的轉換成數組。Array.of(1,2,3)-------[1,2,3]
Array.of(3)------[3]
沒有出現重載的現象,而Array(4)-----[undefined,undefined,undefined,undefined]
Array(1,2)----[1,2]
Array(1,2,3,5)----[1,2,3,5]
3、Array.fill()------->填充數組的中元素
Array(3).fill(4)-------[4,4,4]
Array.fill()可以接收三個參數,第一個是要賦值的元素,第二個是起始位置,第三個是結束位置
['q','w','e'].fill(4,1,2)------['q',2,'e']
4、Array.find()和Array.findIndex()
Array.find()---遍歷數組中的元素找到第一個符合條件的值
可以傳入三個參數:
[1, 5, 10, 15].find(function(value, index, arr) {
return value > 9;
}) // 10
上面代碼中,find
方法的回調函數可以接受三個參數,依次爲當前的值、當前的位置和原數組。
Array.find()和Array.findIndex()這兩個方法都可以查找到NAN而Array.indexOf對於NAN這類的不識別
數組實例的entries(),keys()和values()
ES6提供三個新的方法——entries()
,keys()
和values()
——用於遍歷數組。它們都返回一個遍歷器對象(詳見《Iterator》一章),可以用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']
數組實例的includes()
Array.prototype.includes
方法返回一個布爾值,表示某個數組是否包含給定的值,與字符串的includes
方法類似。該方法屬於ES7,但Babel轉碼器已經支持。
目前在工程中使用的如下:
indexOf
方法有兩個缺點,一是不夠語義化,它的含義是找到參數值的第一個出現位置,所以要去比較是否不等於-1,表達起來不夠直觀。二是,它內部使用嚴格相當運算符(===)進行判斷,這會導致對NaN
的誤判。
[NaN].indexOf(NaN)
// -1
includes
使用的是不一樣的判斷算法,就沒有這個問題。
[NaN].includes(NaN)
// true
5、es6中把空位轉換成undefined,但是es5中定義是比較亂的,最好不要使用空的數組
數組的空位指,數組的某一個位置沒有任何值。比如,Array
構造函數返回的數組都是空位。
Array(3) // [, , ,]
上面代碼中,Array(3)
返回一個具有3個空位的數組。
注意,空位不是undefined
,一個位置的值等於undefined
,依然是有值的。空位是沒有任何值,in
運算符可以說明這一點。
0 in [undefined, undefined, undefined] // true
0 in [, , ,] // false
上面代碼說明,第一個數組的0號位置是有值的,第二個數組的0號位置沒有值。
ES5對空位的處理,已經很不一致了,大多數情況下會忽略空位。
forEach()
,filter()
,every()
和some()
都會跳過空位。map()
會跳過空位,但會保留這個值join()
和toString()
會將空位視爲undefined
,而undefined
和null
會被處理成空字符串。
// forEach方法
[,'a'].forEach((x,i) => console.log(i)); // 1
// filter方法
['a',,'b'].filter(x => true) // ['a','b']
// every方法
[,'a'].every(x => x==='a') // true
// some方法
[,'a'].some(x => x !== 'a') // false
// map方法
[,'a'].map(x => 1) // [,1]
// join方法
[,'a',undefined,null].join('#') // "#a##"
// toString方法
[,'a',undefined,null].toString() // ",a,,"
ES6則是明確將空位轉爲undefined
。
Array.from
方法會將數組的空位,轉爲undefined
,也就是說,這個方法不會忽略空位。
Array.from(['a',,'b'])
// [ "a", undefined, "b" ]
擴展運算符(...
)也會將空位轉爲undefined
。
[...['a',,'b']]
// [ "a", undefined, "b" ]
copyWithin()
會連空位一起拷貝。
[,'a','b',,].copyWithin(2,0) // [,"a",,"a"]
fill()
會將空位視爲正常的數組位置。
new Array(3).fill('a') // ["a","a","a"]
for...of
循環也會遍歷空位。
let arr = [, ,];
for (let i of arr) {
console.log(1);
}
// 1
// 1
上面代碼中,數組arr
有兩個空位,for...of
並沒有忽略它們。如果改成map
方法遍歷,空位是會跳過的。
entries()
、keys()
、values()
、find()
和findIndex()
會將空位處理成undefined
。
// entries()
[...[,'a'].entries()] // [[0,undefined], [1,"a"]]
// keys()
[...[,'a'].keys()] // [0,1]
// values()
[...[,'a'].values()] // [undefined,"a"]
// find()
[,'a'].find(x => true) // undefined
// findIndex()
[,'a'].findIndex(x => true) // 0
由於空位的處理規則非常不統一,所以建議避免出現空位。
數組去重的另外的一個方法:使用set方法,set中都是不重複的數據,再用Array.from()將其轉換成數組
這就提供了去除數組重複成員的另一種方法。
function dedupe(array) {
return Array.from(new Set(array));
}
dedupe([1, 1, 2, 3]) // [1, 2, 3]
擴展運算符和 Set 結構相結合,就可以去除數組的重複成員。
let arr = [3, 5, 2, 2, 5, 5];
let unique = [...new Set(arr)];
// [3, 5, 2]
而且,數組的map
和filter
方法也可以用於
Set 了。