• ES6 - 剩余参数,Array的扩展方法,String的扩展方法


    目录

    一,剩余参数

    1.传不定参数,验证数组的长度。

    2.剩余参数与解构使用

    3.与数组解构使用 函数传对象

    ? 二,Array的扩展方法

    ? 1.扩展语法

    ???1.1?用…输出数组

    1.2? ?合并数组

    1.3 将类数组转为真正的数组

    ?2.小练习

    2.1? 复制数组

    2.2?创建一个函数:用扩展运算符计算两个数的和。

    ?3.Array.from()方法??

    4.?Array.find() 方法

    5.Array.findindex()方法

    6.Array.includes()方法

    三,字符串扩展方法

    1.反引号定义模板

    2.模板字符串换行

    3.模板字符串中调用函数

    4.startsWith()、endsWith()的基本使用方法

    5.repeat字符串重复次数

    6.小例题

    1:找到一组同学中考试分数及格的第一个同学并输出到页面上。

    2:找出大于指定年龄(页面input框接收)的第一个人,并输出这个人的位置?


    一,剩余参数

    剩余参数语法允许我们将一个不定数量的参数表示为一个数组,不定参数定义方式,这种方式很方便的去声明不知道参数情况下的一个函数。

    1.传不定参数,验证数组的长度。

    			function demo(a,...b){
    				console.log(a,b);//b为数组 2,3,4,5
    			}
    			demo(1,2,3,4,5);
    
    • 1
    • 2
    • 3
    • 4

    2.剩余参数与解构使用

    			 let [a,...b] = [1,2,3,4,5];
    			 console.log(a,b);//1  [2,3,4,5]
    
    • 1
    • 2

    3.与数组解构使用 函数传对象

    			 function demo({username,password}){
    				 console.log(username,password);//root  123456
    			 }
    			 demo({username:'root',password:'123456'});
    
    • 1
    • 2
    • 3
    • 4

    二,Array的扩展方法

    1.扩展语法

    **…**扩展运算符可以将数组或者对象转为用逗号分隔的参数序列。

    1.1用…输出数组

                 const arr = [1,2,3];
    			 console.log(...arr); //1,2,3,相当于下面的代码
                 console.log(1,2,3);
    
    • 1
    • 2
    • 3

    1.2 合并数组

    			 const  arr1 = [1,2,3];
    			 const  arr2 = [4,5,6];
    			 const  arr3 = [...arr1,...arr2];
    			 console.log(arr3);// [1,2,3,4,5,6]
    
    • 1
    • 2
    • 3
    • 4

    1.3 将类数组转为真正的数组

    
    
    	
    		
    		
    	
    	
    		
    		
    1
    2
    3
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    2.小练习

    2.1 复制数组

    			 const arr1 = [1,2,3];
    			 const arr2 = [...arr1];
    			 arr2[0]=5;
    			 console.log('这是arr1:'+ arr1+'
    '+'这是arr2:'+arr2);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.2创建一个函数:用扩展运算符计算两个数的和。

    			function demo(a,b){
    				return a+b;
    			}
    			const arr1 = [1,2];
    			const arr2 = [4,5];
    			
    			console.log(demo(...arr1));
    			console.log(demo(...arr2));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3.Array.from()方法

    将伪数组或可遍历对象转换为真正的数组。

        Array.from('12345') // [1,2,3,4,5]
      
        let  arr1 = {
    			1:'a',
    			2:'b',
    			'length':3
            }
            console.log(Array.from(arr1));//undefined ,a,b.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4.Array.find() 方法

    find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。

            let arr1 = [1,2,3,2];
            let target = arr1.find( item => item==2);
            console.log(target);//2,如果未找到,返回undefined
    
                
    
    
    
            let person = [
            
    			    {name:"张三",age:16},
    			    {name:"李四",age:17},
    			    {name:"王五",age:18},
    		    ]
    		
            let target = person.find((item,index)=>{return item.name=='张三'});
            console.log(target.name);//张三
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    5.Array.findindex()方法

    找到符合条件的第一个元素的索引

    let ary = [1, 5, 10, 15];
    let index = ary.findIndex((item, index) => item > 9); 
    console.log(index); // 2
    
    • 1
    • 2
    • 3

    6.Array.includes()方法

    找出某个数组是否包含给定的值。有就返回true 没有就返回false

    			const arr = [1,2,3,4];
    			console.log(arr.includes(10));//false
    
    • 1
    • 2

    三,字符串扩展方法

    使用反引号定义let name = `张三`;

    1.反引号定义模板

    let name = '张三'; 
    let sayHello = `hello,my name is ${name}`; // hello, my name is Lee
    
    • 1
    • 2

    2.模板字符串换行

    		let result = {
    			name: '张三',
    			age: 28,
    			sex: '男'
    		}
    		let html =
    			` 
    		
    ${result.name} ${result.age} ${result.sex}
    ` console.log(html);
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    3.模板字符串中调用函数

    		const sayName = function () {
    			return '张三';
    		};
    		let greet = `${sayName('张三')} ,你好!`;
    		console.log(greet); //张三 ,你好!
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4.startsWith()、endsWith()的基本使用方法

    - startsWith():表示参数字符串是否在原字符串的头部,返回布尔值

    - endsWith():表示参数字符串是否在原字符串的尾部,返回布尔值

            let str = "hello,es6!";
    			console.log(str.startsWith("hello"));
                //判断某个字符串前面是否包含hello 有就为true
    			console.log(str.endsWith("es6!"));//与startsWith相反
    
    • 1
    • 2
    • 3
    • 4

    5.repeat字符串重复次数

    repeat方法表示将原字符串重复n次,返回一个新字符串。

    console.log('hello'.repeat(2));//hellohello
    
    • 1

    6.小例题

    1:找到一组同学中考试分数及格的第一个同学并输出到页面上。

    
    
    
    
        
        
        
        Document
    
    
    
    
        

      第一个及格的学生是:


      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44

      2:找出大于指定年龄(页面input框接收)的第一个人,并输出这个人的位置

      
      
      
      
          
          
          
          Document
      
      
      
      
          

        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
        • 29
        • 30
        • 31
        • 32
        • 33
        • 34
        • 35
        • 36
        • 37
        • 38
        • 39
        • 40
        • 41
        • 42
        • 43
        • 44
        • 45
        • 46
        • 47
        • 48
        • 49
        • 50
        • 51
        • 52
        • 53
        • 54
        • 55

        先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

      • 相关阅读:
        设计模式之访问者模式
        shell 脚本变量
        Python 学习笔记(更新中)
        Python如何爬取免费爬虫ip
        HAproxy:负载均衡
        高级同步机制:Phaser与CountDownLatch详解
        055_末晨曦Vue技术_处理边界情况之内联模板
        select 的使用
        【Java编程进阶】标识符和关键字
        KubeVela 1.4.x 官方文档
      • 原文地址:https://blog.csdn.net/m0_67391907/article/details/126099986