天天看點

數組

數組

擴充操作符

ES6中可使用擴充操作符在一個數組字面量中包含另一個數組

let a = [1, 2, 3]
let b = [0, ...a, 4]    // [0, 1, 2, 3, 4]
// 擴充操作符可用于任何可疊代對象
let digits = [..."adsadassfasfasfsff"] // ["a", "b", "s", ..., "f", "f"]
// 若想去重,可借助集合
let letters = [ ...new Set(digits)]      

稀疏數組

稀疏數組就是數組中有空位,其length值大于實際元素的個數

let c = ["0"]
c[10] = "10"
c[3] = undefined
console.log(c) // [ '0', <2 empty items>, undefined, <6 empty items>, '9' ]      

疊代稀疏數組,會發現空位為undefined,但并非表示空位的值是undefined

for(let i of c){
    console.log(i)  // 空位為undefined
}
console.log(c.length)   // 11
console.log(1 in c) // c[1]為真正的空值,沒有元素,傳回為false
console.log(3 in c) // c[3]為undefined元素,并非真的沒有元素,傳回為true      

forEach可以感覺到稀疏數組,并跳過空位

c.forEach(element=>{
    console.log(element)  // foreach能感覺到稀疏數組,并跳過空位
})      

filter過濾

filter傳入一個回調函數,去除傳回值為false的元素,傳回一個新的數組

let arr = [5, 4, 3, 2, 1]
let arrFilter = arr.filter(x => x<3)  // 過濾掉大于等于三的元素,傳回新資料      

filter會自動過濾掉稀疏數組中的空位,傳回稠密數組

arr.filter(() => true)  // 是以可以使用filter過濾空值      

find與findIndex

let f = arr.find( x => x===3)   // 傳回第一個比對項,找不到傳回undefined
let fIdx = arr.findIndex( x => x > 3)   // 傳回第一個比對項的索引,找不到傳回-1      

every與some

every隻有所有項都比對時,才會傳回true;some隻要有一個比對項,就傳回true

arr.every( x => x <10)
arr.some( x => x === 3)      

reduce 與 reduceRight

歸并,根據條件傳回一個最終結果,reduceRight()是從右向左歸并

arr.reduce((x, y) => x + y) // 條件是求和,那麼就逐一相加,每次的結果将取代x,并進行下一次歸并
arr.reduce((x, y) => (x > y) ? x : y, 100) // 條件為找出最大值,第二個參數為初始的x值,結果為100      

map

map() 方法傳回一個新數組,數組中的元素為原始數組元素調用函數處理後的值。

let strArr = ["hello world", "the definitive guide"]
strArr.map(x => x.split(" "))  // [ [ 'hello', 'world' ], [ 'the', 'definitive', 'guide' ] ]      

flat()與flatMap()打平數組

let matrix = [1, [2, [3, [4]]]]
matrix.flat()  // [ 1, 2, [ 3, [ 4 ] ] ]
matrix.flat(1) // [ 1, 2, [ 3, [ 4 ] ] ]
matrix.flat(2) // [ 1, 2, 3, [ 4 ] ]
matrix.flat(3) // [ 1, 2, 3, 4 ]
matrix.flat(4) // [ 1, 2, 3, 4 ]      

flatMap() 等同于(但效率遠高于) map().flat(),可以把flatMap了解為通用版的map

let strArr = ["hello world", "the definitive guide"]
strArr.flatMap(x => x.split(" ")) // [ 'hello', 'world', 'the', 'definitive', 'guide' ]      

concat()

添加數組,原始數組不變, 可以打平數組,但不會遞歸的打平嵌套的數組

let addArr = [1, 2, 3]
a.concat(4, 5)  // [1, 2, 3, 4, 5]
a.concat([4, 5], [6, 7]) // [1, 2, 3, 4, 5, 6, 7]
a.concat(4, [5, [6, 7]]) // [1, 2, 3, 4, 5, [6, 7]]      

slice()切片

let arr = [5, 4, 3, 2, 1]
arr.slice(0, 2) // [5, 4] [0, 2) 前閉後開
arr.slice(3)    // [2, 1] 從索引3開始到結尾
arr.slice(1, -1) // [4, 3, 2] 支援負索引      

fill()填充

可隻填充切片部分

let farr = new Array(5)
farr.fill(0)    // [0, 0, 0, 0, 0]
farr.fill(9, 1) // [0, 9, 9, 9, 9]
farr.fill(8, 2, -1) // [0, 9, 8, 8, 9]      

copyWithin()

copyWithin(index, startIndex, endIndex),把指定切片複制到指定索引處,會修改原數組, 支援負索引

let arr = [5, 4, 3, 2, 1]
arr.copyWithin(1)   // [5, 5, 4, 3, 2]
arr.copyWithin(2, 3, 5)   // [5, 5, 3, 2, 2]
arr.copyWithin(0, -2)   // [2, 2, 4, 2, 2]      

其他

indexOf lastIndexOf 查找比對項中第一個和最後一個的索引,沒有傳回-1

includes() ES6新方法測試數組中是否包含某個值,傳回true或false

sort( a, b =>{ return a - b })   若 < 0,則a在前,若 > 0,則b在前

arr.join("+")  可以了解為split的反向操作,若不傳參數,則預設為使用","相連

Array.isArray([]) 用于确定一個未知值是否是數組,傳回true或false

arr.length = 3  修改數組長度會截斷數組

下一篇: 抽象和接口