<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          整點簡單的,分享10個數(shù)組工具類

          共 4909字,需瀏覽 10分鐘

           ·

          2021-06-24 06:41

          1.數(shù)組并集

          const union = function (a, b, k) {
          return a.concat(b.filter(i => (k ? !a.map(i => i[k]).includes(i[k]) : !a.includes(i))))
          }
          復制代碼

          示例:

          let a = [1, 2, 3, 4, 5]
          let b = [1, 2, 4, 5, 6]
          union(a, b) //[1,2,3,4,5,6]

          // 場景2:
          let a1 = [
          { id: 1, name: '張三', age: 20 },
          { id: 2, name: '李四', age: 21 },
          { id: 3, name: '小二', age: 23 }
          ]
          let b1 = [
          { id: 2, name: '李四', age: 21 },
          { id: 4, name: '小明', age: 24 },
          { id: 5, name: '小紅', age: 25 }
          ]

          // 通過 id 獲取并集
          union(a1, b1, 'id')
          /*
          [
          {id: 1, name: "張三", age: 20}
          {id: 2, name: "李四", age: 21}
          {id: 3, name: "小二", age: 23}
          {id: 4, name: "小明", age: 24}
          {id: 5, name: "小紅", age: 25}
          ]
          */

          復制代碼

          2.數(shù)組交集

          const intersection = function (a, b, k) {
          return a.filter(t => (k ? b.map(i => i[k]).includes(t[k]) : b.includes(t)))
          }
          復制代碼

          示例:

          let a = [1, 2, 3, 4, 5]
          let b = [1, 2, 4, 5, 6]
          intersection(a, b) // [1,2,4,5]

          // 場景2:
          let a1 = [
          { id: 1, name: '張三', age: 20 },
          { id: 2, name: '李四', age: 21 },
          { id: 3, name: '小二', age: 23 }
          ]
          let b1 = [
          { id: 2, name: '李四', age: 21 },
          { id: 4, name: '小明', age: 24 },
          { id: 5, name: '小紅', age: 25 }
          ]
          intersection(a1, b1, 'id') //[ { id: 2, name: '李四', age: 21 }]
          復制代碼

          3.數(shù)組差集

          const except = function (a, b, k) {
          return [...a, ...b].filter(i => ![a, b].every(t => (k ? t.map(i => i[k]).includes(i[k]) : t.includes(i))))
          }
          復制代碼

          示例:

          let a = [1, 2, 3, 4, 5]
          let b = [1, 2, 4, 5, 6]

          except(a, b) // [3,6]

          let a1 = [
          { id: 1, name: '張三', age: 20 },
          { id: 2, name: '李四', age: 21 },
          { id: 3, name: '小二', age: 23 }
          ]
          let b1 = [
          { id: 2, name: '李四', age: 21 },
          { id: 4, name: '小明', age: 24 },
          { id: 5, name: '小紅', age: 25 }
          ]


          except(a1, b1, 'id')
          /*
          [
          {id: 1, name: "張三", age: 20}
          {id: 3, name: "小二", age: 23}
          {id: 4, name: "小明", age: 24}
          {id: 5, name: "小紅", age: 25}
          ]
          */

          復制代碼

          4.數(shù)組分組

          /**
          * @description: 一維數(shù)組轉二維數(shù)組 (分組)
          * @param {Array} arr:數(shù)組
          * @param {Number} num: 平分基數(shù)(num 個為一組進行分組(歸檔))
          */

          const group = function (arr, num) {
          return [...Array(Math.ceil(arr.length / num)).keys()].reduce((p, _, i) => (p.push(arr.slice(i * num, (i + 1) * num)), p), [])
          }
          復制代碼

          示例:

            group([1,2,3,4,5,6,7,8,9,10],2) // [[1,2],[3,4],[5,6],[7,8],[9.10]]

          group([1,2,3,4,5,6,7,8,9,10],3) // [[1,2,3],[4,5,6],[7,8,9],[10]]
          復制代碼

          5.數(shù)組平均數(shù)

          /**
          * 數(shù)組平均數(shù)
          * @param {Array} a:數(shù)組
          * @param {Function | String} f:函數(shù) 或 key
          */

          const mean = function (a, f) {
          return (f ? a.map(typeof f === 'function' ? f : v => v[f]) : a).reduce((acc, val) => acc + val * 1, 0) / a.length
          }
          復制代碼

          示例:

            mean([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n) // 5
          mean([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n') // 5
          mean([4, 2, 8, 6]) // 5
          mean(['4', 2, '8', 6]) // 5
          復制代碼

          6.數(shù)組生成

          /**
          * @description: 生成 起止數(shù)字間(包含起止數(shù)字)的升序數(shù)組
          * @param {Number} min : 最小值
          * @param {Number} max :最大值
          */

          const range = function (min, max) {
          return Array.from({ length: max - min + 1 }, (_, i) => i + min)
          }
          復制代碼

          示例:

           range(0,10) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
          range(1,9) // [1, 2, 3, 4, 5, 6, 7, 8, 9]

          復制代碼

          7.數(shù)組求和

          const sum = function (a, k) {
          return a.reduce((p, c) => p + (k ? c[k] || 0 : c), 0)
          }
          復制代碼

          示例:

          let a = [1, 2, 3, 4, 5]
          sum(a) // 15

          let a1 = [
          { id: 1, name: '張三', age: 20 },
          { id: 2, name: '李四', age: 21 },
          { id: 3, name: '小二', age: 23 }
          ]
          sum(a1, 'age') // 64
          復制代碼

          8.數(shù)組扁平化

          /**
          * 指定深度扁平化數(shù)組
          * @param {Array} arr :扁平化的數(shù)組
          * @param {Number} depth:扁平化的層級
          */

          const flatten = function (arr, depth = 1) {
          return arr.reduce((a, v) => a.concat(depth > 1 && Array.isArray(v) ? flatten(v, depth - 1) : v), [])
          }
          復制代碼

          示例:

           flatten([1, 2, 3, [4, [5, 6, [7]]]]) //[1, 2, 3, 4, [5,6,[7]]]

          flatten([1, 2, 3, [4, [5, 6, [7]]]], 2) //[1, 2, 3, 4, 5,6,[7]]
          復制代碼

          9. 數(shù)組值位置交換

          /**
          * @description: 交換數(shù)組中任一兩個值的位置
          * @param {Array} arr:數(shù)組
          * @param {Number} oldIndex:老位置索引
          * @param {Number} newIndex:新位置索引
          * @param {Boolean} isChangeOldArr: 是否改變原數(shù)組
          * @return {Array} 返回一個數(shù)組
          */

          const exchangePostion = function (arr, oldIndex, newIndex, isChangeOldArr = false) {
          let a = isChangeOldArr ? arr : JSON.parse(JSON.stringify(arr))
          a.splice(oldIndex, 1, a.splice(newIndex, 1, a[oldIndex])[0])
          return a
          }
          復制代碼

          示例:

           let a1 = [1, 2, 3, 4, 5, 6]

          exchangePostion(a1, 4, 1)// [1, 5, 3, 4, 2, 6]

          a1 //[1, 2, 3, 4, 5, 6]

          let a1 = [1, 2, 3, 4, 5, 6]

          exchangePostion(a1, 4, 1,true)// [1, 5, 3, 4, 2, 6]

          a1 // [1, 5, 3, 4, 2, 6]
          復制代碼

          10.數(shù)組歸檔

          /**
          * @description: 對一維 json 數(shù)組進行歸檔(根據(jù) key)
          * @param {Array} arr:一維數(shù)組
          * @param {String} key:key 字符串
          */

          const archive = function (arr, key) {
          return Array.from(new Set(arr.map(i => i[key]))).reduce((p, c) => (p.push(arr.filter(i => i[key] === c)), p), [])
          }
          復制代碼

          示例:

          let books = [ {date:'1月',name:'地理書'}, {date:'1月',name:'歷史書'}, {date:'2月',name:'化學書'} ]

          archive( books, 'date')
          // [[{date:'1月',name:'地理書'},{date:'1月',name:'歷史書'}],[ {date:'2月',name:'化學書'}]]
          復制代碼


          轉自:https://juejin.cn/post/6975846295059562503



          瀏覽 41
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  一级a一级a爱片免费观看 | igao在线视频 | 青草青视频在线 | 久久久久久99精品无码 | 免费看又色又爽又黄的成人用品 |