zoukankan      html  css  js  c++  java
  • 8-ES6之数组

     <script>
            //数组的方法from() of()
            //from()将伪数组转换成真正的数组
            //ES5写法将伪数组转换成真正的数组
            function add(){
                let arr=[].slice.call(arguments);
                console.log(arr)
            }
            add(1,2,3)
    
            //ES6写法
            function add(){
                let arr=Array.from(arguments)
                console.log(arr)
            }
            add(1,2,3)
    
            let lis = document.querySelectorAll('li');
            //转换成数组
            console.log(Array.from(lis));
            //转换成数据,更简洁的写法,扩展运算符换成真正的数组
            console.log([...lis])
    
            //from(),还可以传递第二个参数(函数),用来对每个元素进行处理
            Array.from(lis,ele =>ele.textContent)
            console.log(lis)
    
            //2.of()将任意的数据类型,转换成数组
            console.log(Array.of(3,11,20,[1,2,3]));
           
            // 3 copywithin()将数据内部元素拷贝到其他位置
            let arr1= [1,2,3,8,9].copyWithin(0,3) // [8, 9, 3, 8, 9] 从3位置往后把0位置元素都替换
    
            //find() findIndex()查找元素,查询索引,参数是个回调函数
            let arr2=[1,2,-10,-20,9,2].find( n=> n<0)
            console.log(arr2)
     
            //entries(),keys,values()返回一个遍历器,可以用for ..of 循环访问
            for(let index of [1,2].keys()){
                console.log(index);
            }
            for(let val of [1,2].values()){
                console.log(val);
            }
            //entries返回所发索引和值
            for(let [index,ele] of [1,2].entries()){
                console.log(index,ele);
            }     ;
    
            let lerrer = ['a','b','c']
    
            //6  inculdes(),返回一个布尔值,表示数组是否含某个值
            console.log(lerrer.includes('a'))
    
            //
    
        </script>
    

      

  • 相关阅读:
    Spring data jpa使用枚举
    IO
    Tomcat相关问题
    flex中为控件添加监听器并计算
    导出excel
    webService常见问题
    从指定的路径中查找含有特殊字符串的文件
    flex中日期的格式化
    flex与后台及页面间对象的传递
    打印时有选择的打印
  • 原文地址:https://www.cnblogs.com/lixiang1013/p/13657664.html
Copyright © 2011-2022 走看看