• vue中三种for循环与高阶函数(filter、map、reduce)的使用


    for循环的三种用法

    1、第一种是我们平常用的最多的for循坏 如: 普通的for循环 ,显得繁琐,i为for为数组赋予的序号

    	let totalPrice = 0
    	for (let i = 0; i < this.books.length; i++) {
    	   totalPrice += this.books[i].price * this.books[i].count
    	}
    
    • 1
    • 2
    • 3
    • 4

    2、第二种for in 循坏,in对应的是数组对象名字,i 也是数组的序号

    	let totalPrice = 0
    	  for (let i in this.books) {
    	    const book = this.books[i]
    	    totalPrice += book.price * book.count
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3、第三种,item代表的是数组里具体的其中一个元素

    	let totalPrice = 0
    	  for (let item of this.books) {
    	  totalPrice += item.price * item.count
    	}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    显然第三种for 更直接有简便,下面用这第三种for循环来实现对数组进行判断,改变和汇总操作

    1、需求: 取出所有小于100的数字

    let newNums = []
      for (let n of nums) {
        if (n < 100) {
          newNums.push(n)
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、需求:将所有小于100的数字进行转化: 全部*2

    let new2Nums = []
      for (let n of newNums) {
        new2Nums.push(n * 2)
    }
    console.log(new2Nums);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3、需求:将所有new2Nums数字相加,得到最终的记过

    let total = 0
      for (let n of new2Nums) {
        total += n
    }
    console.log(total);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    上面的操作虽然实现了这三种需求,但是还是太繁琐,使用高阶函数(filter、map、reduce)可以更便捷的实现需求
    第一种是filter的使用:参数是回调函数
    filter中的回调函数有一个要求: 必须返回一个boolean值
    true: 当返回true时, 函数内部会自动将这次回调的n加入到新的数组中
    false: 当返回false时, 函数内部会过滤掉这次的n

    const nums = [10, 20, 111, 222, 444, 40, 50]
     let newNums = nums.filter(function (n) {
      return n < 100  //这里返回了 10 20 40 50
     })
    console.log(newNums);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    第二种是map函数的使用:对数组进行遍历了一遍,并且return 回数组 参数也是回调函数

    
    // 20, 40, 80, 100
    let new2Nums = newNums.map(function (n) { // 20
      return n * 2
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5

    第三种是reduce函数的使用 ,参数有两个,还分别是回调函数 和 preValue 的初始值
    reduce作用对数组中所有的内容进行汇总, reduce和迭代方法(map、fliter、forEach)一样,会遍历过滤数组;reduce()方法会遍历数组中的每一个元素,每遍历一次就会执行一次回调函数。当遍历完之后会将最后的结果返回出去。

    
    let total = new2Nums.reduce(function (preValue, n) {
      return preValue + n   //这个preValue就代表上一个值,比如当 preValue 0 ,n 为20 时候 返回20
                            //后面当  preValue就等于 20 ,那么 n 自然就为40  结果是60
    }, 0)    
    console.log(total);
    //第一次: preValue 0 n 20
    //第二次: preValue 20 n 40
    //第二次: preValue 60 n 80
    //第二次: preValue 140 n 100
    //total:240
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    Node.js事件循环
    ArrayList中放的是一个对象,如何同时根据对象中的三个字段对List进行排序
    Vue框架快速上手指南:打通前端开发新大门
    Hadoop 配置 Kerberos 认证
    python爬虫中怎么写反爬虫
    PHP 框架详解 - Symfony 框架
    W5500+树莓派RP2040入门教程之MQTT篇(十二)
    几个测试接口的好工具,效率加倍~
    【cmake】彻底弄懂cmake中的可见性/传递性 INTEFACE 问题
    以字符串mark作为分隔符,对字符串s进行分割
  • 原文地址:https://blog.csdn.net/a1k2l45k/article/details/128187934