美文网首页
箭头函数和数组

箭头函数和数组

作者: 田成力 | 来源:发表于2019-10-09 20:34 被阅读0次

箭头函数&数组

箭头函数

特点: 没有 this 没有 arguments 没有prototype
在箭头函数中使用到的以上关键字都是上一级的关键字

数组


// reduce小例子:
let arr = [1, 2, 3, 4, 5];

//pre 上一次reduce返回的结果
//current 当前项
//index 当前项的下标
//arr 原数组
let total = arr.reduce((pre, current, index, arr) => {
  return pre + current;
})
console.log(total);

//小技巧:
// 如何使用reduce将有下面的数组需要求出和
let arr=[{price:10,count:5},{price:10,count:5},{price:10,count:5},{price:10,count:5},{price:10,count:5}]

//如果使用
arr.reduce((pre,current,index,arr)=>{
  return pre.price*pre.count+current.price*current.count;
})
//会发现有bug 因为第二次的pre已经是一个数值了而不是一个对象所以点击price的时候回出现错误
//这时候我们可以,手动的将arr头部添加一个0
let arr=[0,{price:10,count:5},{price:10,count:5},{price:10,count:5},{price:10,count:5},{price:10,count:5}]
arr.reduce((pre,current,index,arr)=>{
  return pre+current.price*current.count;
})


//更优雅的方式:
let arr=[{price:10,count:5},{price:10,count:5},{price:10,count:5},{price:10,count:5},{price:10,count:5}]
arr.reduce((pre,current,index,arr)=>{
  return pre+current.price*current.count;
},0)//手动指定第一项的pre的值

如何实现数组的flat的功能

//flat的功能:
let arr = [1, [2, 3, [4, 5, [6, 7, 8]]]];
let newArr=arr.flat(100)
console.log(newArr);


let arr = [1, [2, 3, [4, 5, [6, 7, 8]]]];



Array.prototype.flat = function () {
  return this.reduce((pre, current, index, arr) => {
    if (Array.isArray(current)) {
      //如果当前项是数组
       pre.push(...current.flat())
       return pre;
    } else {
      console.log(pre);
      console.log(current);
      pre.push(current);
      return pre;
    }
  }, [])
}

 console.log("1111",arr.flat(1000));

利用reduce实现componse的方法

//没有compose函数的用法
function sub(a, b) {
  return a + b;
}

function len(str) {
  return str.length;
}

function addRMB(len) {
  return '$' + len;
}

//想组合调用三个函数
let ret = addRMB(len(sub('abc', 'bcd')));
console.log(ret);


//想要实现:
let ret_compose = compose(addRMB, len, sub)('abc', 'bcd');

function compose(...fns) {
  //找到第一个需要执行的函数
  let lastfn = fns.pop();
  return function (...args) {
   return fns.reduceRight((pre, current) => {
      return current(pre);
    }, lastfn(...args))
  }
}
console.log(ret_compose);

自己实现reduce

  let arr = [{ price: 10, count: 5 }, { price: 10, count: 5 }, { price: 10, count: 5 }];

  Array.prototype.reduce = function (fn, defaultPre) {
    let oldArr = JSON.parse(JSON.stringify(arr));
    let pre = null;
    if (defaultPre != null && defaultPre != undefined) {
      pre = defaultPre;
    } else {
      pre = arr.shift();
    }
    arr.forEach((item, index) => {
      pre = fn(pre, item, index, oldArr);
    });
    return pre;
  }
  let total = arr.reduce((pre, current, index, arr) => {
    return pre + current.price * current.count;
  }, 0)
  console.log(total);

相关文章

  • 箭头函数和数组

    箭头函数&数组 箭头函数 特点: 没有 this 没有 arguments 没有prototype在箭头函数中使用...

  • reduce

    数组里所有值的和 你也可以写成箭头函数的形式: 将二维数组转化为一维 你也可以写成箭头函数的形式: 计算数组中每个...

  • ES6学习笔记

    1. 变量的解构赋值 数组解构 对象解构 2. 箭头函数,rest参数 箭头函数 rest 参数 3. 数组: 扩...

  • ES6 概述(2)

    数组的解构和赋值 二维数组 对象定义:一组无序属性的集合 箭头函数 有名函数

  • 箭头函数和立即执行函数

    箭头函数 箭头函数和普通函数有什么区别?如果把箭头函数转换为不用箭头函数的形式,如何转换主要是this的差别,箭头...

  • 2019-01-11

    ES6 箭头函数 箭头函数表示法:()=>console.log('Hello') 箭头函数和普通函数的区别 和普...

  • 伪数组转数组实现方式

    伪数组转数组方法 伪数组也叫类数组。像函数中的arguments(箭头函数除外)或者 一组元素返回的集合。 有时操...

  • js理解普通函数和箭头函数

    普通函数: 箭头函数: 区别: 构造函数和原型 箭头函数不能作为构造函数 不能new。会报错 箭头函数没有原型属性...

  • 写更漂亮的javascript

    用更合理的方式写 JavaScript 目录 声明变量 对象 数组 字符串 函数 箭头函数 模块 迭代器和生成器 ...

  • 2021-01-22

    前端学习Day 1 熟悉BOM/DOM 数据类型之间的转换 数组的几种API调用 传统函数和箭头函数的区别 // ...

网友评论

      本文标题:箭头函数和数组

      本文链接:https://www.haomeiwen.com/subject/qxhnpctx.html