1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

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

        共 3023字,需瀏覽 7分鐘

         ·

        2021-10-17 21:56

        來源 |?http://www.fly63.com/


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

        Object.keys()返回對象鍵Object.values()的數(shù)組,返回對象值Object.entries()的數(shù)組,并以格式返回對象的鍵和相應(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)賦值來迭代結(jié)果非常方便。

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

        解構(gòu)賦值允許您從數(shù)組或?qū)ο笾刑崛≈挡⑺鼈兎峙浣o變量。在這種情況下,const [key, value]意味著不是將[key, value]數(shù)組分配給 ,而是將該數(shù)組element的第一個元素分配給key,將第二個元素分配給value。它相當(dāng)于:

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

        如您所見,解構(gòu)使這變得更加簡單。

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

        如果指定的回調(diào)函數(shù)為數(shù)組的每個元素every()返回,true則該方法返回。如果指定的回調(diào)函數(shù)為某個(至少一個)元素返回,則該方法返回。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()方法返回滿足提供的回調(diào)函數(shù)的第一個元素。該filter()方法返回滿足提供的回調(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()方法返回一個數(shù)組,其中包含對數(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()方法通過調(diào)用提供的具有兩個元素的回調(diào)函數(shù)將數(shù)組縮減為單個值。

        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()方法采用可選的第二個參數(shù),即初始值。當(dāng)您調(diào)用的數(shù)組reduce()可以有零個或一個元素時,這很有用。例如,如果我們想創(chuàng)建一個函數(shù)sum(),它接受一個數(shù)組作為參數(shù)并返回所有元素的總和,我們可以這樣寫:

        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í)更多技能

        請點擊中國公眾號

        瀏覽 49
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            一女两男三p吞精喝尿 | 婷婷性爱网 | 动漫美女被男生操 | 一级性爱电影网站 | 无码人妻一区二区三区免费九色 | 波多野结衣一区二区三区在线 | 免费网站成人 视频在线观看 | 意大利性经典xxxxx | 人妻懂色av粉嫩av浪潮av | 一级无码片 |