zoukankan      html  css  js  c++  java
  • 数组中every、some、from、reduce四个很重要的api用法总结

    • every

      • 应用场景:数组的每一项是否都符合要求,都符合返回true,有一项不符合返回false
      • [1,2,3,4,5,6,7].every(item=>item>1)//false
    • some

      • 应用场景:数组的是否有某一项符合要求,只要寻址到有一项符合返回true,都不符合返回false
      • [1,2,3,4,5,6,7].some(item=>item>1)//true
    • from

      • 对拥有length属性的对象或可迭代的对象来返回一个数组
      • Array.from(new Set([1,1,2,3])) //[1,2,3]
      • Array.from([1,1,2,3],X=>X*10) //[10,10,20,30]
    • reduce【函数累计处理的结果】

      • api:arr.reduce(callback(accumulator, currentValue[, index[, array]])[, initialValue])
      • callback
      • initialValue可选
        • 作为第一次调用 callback函数时的第一个参数的值。 如果没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错
      • 关于第一次执行:
        • 回调函数第一次执行时,accumulator 和currentValue的取值有两种情况:如果调用reduce()时提供了initialValue,accumulator取值为initialValue,currentValue取数组中的第一个值;如果没有提供 initialValue,那么accumulator取数组中的第一个值,currentValue取数组中的第二个值。
      • 提供初始值通常更安全
            // reduce() 没有初始值
            [ { x: 2 }, { x: 22 }, { x: 42 } ].reduce( (a,b) => Math.max( a.x, b.x ) ); // NaN
            [ { x: 2 }, { x: 22 }            ].reduce( (a,b) => Math.max( a.x, b.x ) ); // 22
            [ { x: 2 }                       ].reduce( (a,b) => Math.max( a.x, b.x ) ); // { x: 2 }
            [                                ].reduce( (a,b) => Math.max( a.x, b.x ) ); // TypeError
        
        • 常见应用场景
          • 求和,求乘积
              var  arr = [1, 2, 3, 4];
              var sum = arr.reduce((x,y)=>x+y)//10
              var mul = arr.reduce((x,y)=>x*y)//24
          
          • 求对象里的属性求和
          var result = [
              {
                  subject: 'math',
                  score: 10
              },
              {
                  subject: 'chinese',
                  score: 20
              },
              {
                  subject: 'english',
                  score: 30
              }
          ];
          
          var sum = result.reduce((a, b)=> a + b.score, 0));//60
          
          • 数组去重
          let arr = [1,2,3,4,4,1]
          let newArr = arr.reduce((a,b)=>{
              if(!a.includes(b)){
                return a.concat(b)
              }else{
                return b
              }
          },[])//[1,2,3,4]
          
          • 拉平数组
          let arr = [[0, 1], [2, 3], [4, 5]]
          let newArr = arr.reduce((a,b)=>{
              return a.concat(b)
          },[])//[0,1,2,3,4,5]
          
          • 拉平多维数组
          let arr = [[0, 1], [2, 3], [4,[5,6,7]]]
          const newArr = function(arr){
             return arr.reduce((a,b)=>a.concat(Array.isArray(b)?newArr(b):b),[])
          }
          newArr(arr)//[0,1,2,3,4,5,6,7]
          
          • 计算数组中每个元素出现的次数
          let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
          let nameNum = names.reduce((pre,cur)=>{
            if(cur in pre){
              pre[cur]++
            }else{
              pre[cur] = 1 
            }
            return pre
          },{}) // {'Alice':2,'Bob':1,'Tiff':1,'Bruce':1}
          
  • 相关阅读:
    介绍一款jquery ui组件gijgo(含tree树状结构、grid表格),特点:简易、文档全清晰易懂、示例代码
    【未完待续】API接口
    表单中Readonly和Disabled的区别:readonly在get和post可传值到后端,disabled不可
    Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案
    一种历史详细记录表,完整实现:CommonOperateLog 详细记录某用户、某时间、对某表、某主键、某字段的修改(新旧值
    js return falsee.preventDefault() 以及session
    bootstrape学习
    Redis的PHP操作手册
    PHP表单常用正则表达式(URL、HTTP、手机、邮箱等)
    大型网站架构演化
  • 原文地址:https://www.cnblogs.com/yogic/p/14769399.html
Copyright © 2011-2022 走看看