Object對(duì)象和數(shù)組有很多內(nèi)置方法

來(lái)源 |?http://www.fly63.com/
Object.keys(),?Object.values()和Object.entries()
Object.keys()返回對(duì)象鍵Object.values()的數(shù)組,返回對(duì)象值Object.entries()的數(shù)組,并以格式返回對(duì)象的鍵和相應(yīng)值的數(shù)組[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 循環(huán)和解構(gòu)賦值
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 循環(huán)和解構(gòu)賦值來(lái)迭代結(jié)果非常方便。
For-of 循環(huán)可讓您迭代數(shù)組元素。語(yǔ)法是for (const element of array)(我們可以const用var或替換let,但const如果我們不打算修改 ,最好使用element)。
解構(gòu)賦值允許您從數(shù)組或?qū)ο笾刑崛≈挡⑺鼈兎峙浣o變量。在這種情況下,const [key, value]意味著不是將[key, value]數(shù)組分配給 ,而是將該數(shù)組element的第一個(gè)元素分配給key,將第二個(gè)元素分配給value。它相當(dāng)于:
for (const element of Object.entries(obj)) {const key = element[0],value = element[1]}
如您所見(jiàn),解構(gòu)使這變得更加簡(jiǎn)單。
Array.prototype.every()?和?Array.prototype.some()
如果指定的回調(diào)函數(shù)為數(shù)組的每個(gè)元素every()返回,true則該方法返回。如果指定的回調(diào)函數(shù)為某個(gè)(至少一個(gè))元素返回,則該方法返回。truesome()truetrue
const arr = [1, 2, 3]// true, because every element is greater than 0console.log(arr.every(x => x > 0))// false, because 3^2 is greater than 5console.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 5console.log(arr.some(x => x === 5))
Array.prototype.find()?和?Array.prototype.filter()
這些find()方法返回滿(mǎn)足提供的回調(diào)函數(shù)的第一個(gè)元素。該filter()方法返回滿(mǎn)足提供的回調(diào)函數(shù)的所有元素的數(shù)組。
const arr = [1, 2, 3]// 2, because 2^2 !== 2console.log(arr.find(x => x !== Math.pow(x, 2)))// 1, because it's the first elementconsole.log(arr.find(x => true))// undefined, because none of the elements equals 7console.log(arr.find(x => x === 7))// [2, 3], because these elements are greater than 1console.log(arr.filter(x => x > 1))// [1, 2, 3], because the function returns true for all elementsconsole.log(arr.filter(x => true))// [], because none of the elements equals neither 6 nor 7console.log(arr.filter(x => x === 6 || x === 7))
Array.prototype.map()
該map()方法返回一個(gè)數(shù)組,其中包含對(duì)數(shù)組元素調(diào)用提供的回調(diào)函數(shù)的結(jié)果。
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()方法通過(guò)調(diào)用提供的具有兩個(gè)元素的回調(diào)函數(shù)將數(shù)組縮減為單個(gè)值。
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()方法采用可選的第二個(gè)參數(shù),即初始值。當(dāng)您調(diào)用的數(shù)組reduce()可以有零個(gè)或一個(gè)元素時(shí),這很有用。例如,如果我們想創(chuàng)建一個(gè)函數(shù)sum(),它接受一個(gè)數(shù)組作為參數(shù)并返回所有元素的總和,我們可以這樣寫(xiě):
const sum = arr => arr.reduce((a, b) => a + b, 0)console.log(sum([])) // 0console.log(sum([4])) // 4console.log(sum([2, 5])) // 7
學(xué)習(xí)更多技能
請(qǐng)點(diǎn)擊中國(guó)公眾號(hào)
![]()

