天天看點

Object對象和數組有很多内置方法

Object對象和數組有很多内置方法

Object.keys(), Object.values()和Object.entries()

Object.keys()傳回對象鍵Object.values()的數組,傳回對象值Object.entries()的數組,并以格式傳回對象的鍵和相應值的數組[key, value]。

const obj = {
  a: 1
 ,b: 2
 ,c: 3
}


console.log(Object.keys(obj)) // ['a', 'b', 'c']
console.log(Object.values(obj)) // [1, 2, 3]
console.log(Object.entries(obj)) // [['a', 1], ['b', 2], ['c', 3]]      

Object.entries() 使用 for-of 循環和解構指派

const obj = {
  a: 1
 ,b: 2
 ,c: 3
}


for (const [key, value] of Object.entries(obj)) {
  console.log(`key: ${key}, value: ${value}`)
}      

Object.entries()用for-of 循環和解構指派來疊代結果非常友善。

For-of 循環可讓您疊代數組元素。文法是for (const element of array)(我們可以const用var或替換let,但const如果我們不打算修改 ,最好使用element)。

解構指派允許您從數組或對象中提取值并将它們配置設定給變量。在這種情況下,const [key, value]意味着不是将[key, value]數組配置設定給 ,而是将該數組element的第一個元素配置設定給key,将第二個元素配置設定給value。它相當于:

for (const element of Object.entries(obj)) {
  const key = element[0]
       ,value = element[1]
}      

如您所見,解構使這變得更加簡單。

Array.prototype.every() 和 Array.prototype.some()

如果指定的回調函數為數組的每個元素every()傳回,true則該方法傳回。如果指定的回調函數為某個(至少一個)元素傳回,則該方法傳回。truesome()truetrue

const arr = [1, 2, 3]


// true, because every element is greater than 0
console.log(arr.every(x => x > 0))
// false, because 3^2 is greater than 5
console.log(arr.every(x => Math.pow(x, 2) < 5))
// true, because 2 is even (the remainder from dividing by 2 is 0)
console.log(arr.some(x => x % 2 === 0))
// false, because none of the elements is equal to 5
console.log(arr.some(x => x === 5))      

Array.prototype.find() 和 Array.prototype.filter()

這些find()方法傳回滿足提供的回調函數的第一個元素。該filter()方法傳回滿足提供的回調函數的所有元素的數組。

const arr = [1, 2, 3]


// 2, because 2^2 !== 2
console.log(arr.find(x => x !== Math.pow(x, 2)))
// 1, because it's the first element
console.log(arr.find(x => true))
// undefined, because none of the elements equals 7
console.log(arr.find(x => x === 7))


// [2, 3], because these elements are greater than 1
console.log(arr.filter(x => x > 1))
// [1, 2, 3], because the function returns true for all elements
console.log(arr.filter(x => true))
// [], because none of the elements equals neither 6 nor 7
console.log(arr.filter(x => x === 6 || x === 7))      

Array.prototype.map()

該map()方法傳回一個數組,其中包含對數組元素調用提供的回調函數的結果。

const arr = [1, 2, 3]


console.log(arr.map(x => x + 1)) // [2, 3, 4]
console.log(arr.map(x => String.fromCharCode(96 + x))) // ['a', 'b', 'c']
console.log(arr.map(x => x)) // [1, 2, 3] (no-op)
console.log(arr.map(x => Math.pow(x, 2))) // [1, 4, 9]
console.log(arr.map(String)) // ['1', '2', '3']      

Array.prototype.reduce()

該reduce()方法通過調用提供的具有兩個元素的回調函數将數組縮減為單個值。

const arr = [1, 2, 3]


// Sum of array elements.
console.log(arr.reduce((a, b) => a + b)) // 6
// The largest number in the array.
console.log(arr.reduce((a, b) => a > b ? a : b)) // 3      

該reduce()方法采用可選的第二個參數,即初始值。當您調用的數組reduce()可以有零個或一個元素時,這很有用。例如,如果我們想建立一個函數sum(),它接受一個數組作為參數并傳回所有元素的總和,我們可以這樣寫:

const sum = arr => arr.reduce((a, b) => a + b, 0)


console.log(sum([]))     // 0
console.log(sum([4]))    // 4
console.log(sum([2, 5])) // 7      

學習更多技能

請點選中國公衆号