• es6 数组的扩展


    var arrayLike = {
      '0': 'a',
      '1': 'b',
      '2': 'c',
      'length': 3
    }
    
    var arr1 = [].slice.call(arrayLike);
    console.log(arr1);  //['a','b','c']
    
    var arr2 = Array.from(arrayLike);
    console.log(arr2);  //['a','b','c']
    
    var arr3 = Array.from('hello');
    console.log(arr3);  //["h", "e", "l", "l", "o"]
    
    let nameSet = new Set(['a','b','c','a'])
    console.log(nameSet); //{"a", "b", "c"}
    
    var arr4 = Array.from(nameSet);
    console.log(arr4);  //["a", "b", "c"]
    
    function foo(){
      var arr = Array.from(...arguments)
      console.log(arr);
    }
    
    foo([1,2,3,4,5,6])  //[1, 2, 3, 4, 5, 6]
    
    var arr5 = Array.from({length: 3});
    console.log(arr5);  //[undefined, undefined, undefined]
    
    const toArray = (() => 
      Array.from? Array.from: obj => [].slice.call(obj) // obj => [].slice.call(obj)
    )()
    
    Array.from([1,2,3], x=>x*x);
    
    Array.from([1,2,3]).map(x=>x*x);
    
    Array.from([1,2,3], (x)=>x*x);
    
    //-----------Array.of
    console.log(Array.of(1,2,3)); //[1,2,3]
    console.log(Array.of(3)); //[3]
    console.log(Array.of(3).length);  //1
    console.log(Array.of());  //[]
    
    //copyWithin
    var cw = [1,2,3,4,5,6,7,8,9].copyWithin(0, 5);  //[6,7,8,9,5,6,7,8,9]
    console.log(cw);
    var cw = [1,2,3,4,5,6,7,8,9].copyWithin(0,5,6); //[6,2,3,4,5,6,7,8,9]
    console.log(cw);
    var cw = [1,2,3,4,5,6,7,8,9].copyWithin(1,5,6); //[1,6,3,4,5,6,7,8,9]
    console.log(cw);
    
    //find() findIndex()
    var arr6 = [1,2,3,4,5,6,7,8,9].find((n) => n > 3)
    console.log(arr6);  //4
    var arr7 = [1,2,3,4,5,6,7,8,9].find(function(n){
      return n > 5;
    });
    console.log(arr7);  //6 //返回第一个匹配的值 //findIndex返回第一个匹配的值的位置
    
    //fill
    var arr8 = ['a','b','c'].fill(7)
    console.log(arr8); //[8,8,8]
    
    var arr9 = [1,2,3,4,5,6,7,8,9].fill('aa' , 2,4);
    console.log(arr9);  //[1,2,'aa','aa',5,6,7,8,9]
    
    //entries keys values
    for(let key of ['a','b','c'].keys()){
      console.log(key);
    }
    //0,1,2
    
    for(let value of ['a','b','c'].values()){
      console.log(value);
    }
    //a,b,c
    for(let entry of ['a','b','c'].entries()){
      console.log(entry);
    }
    //[0, 'a'][1, 'b'][2, 'c']
    
    //数组includes
    console.log([1,2,3].includes[2]); //true
    console.log([1,2,3].includes[4]); //false
    
    //空数组
    console.log(Array(3)) //[undefined × 3] //书上说返回[,,,]但是我确返回了[undefined × 3]
    console.log(0 in [undefined,undefined,undefined]) //true
    console.log(0 in [,,,]);  //false
    
    //数组推导
    var a1 = [1,2,3,4];
    //var a2 = [for(n of a1)n*2] 报错了了
    //console.log(a2);
  • 相关阅读:
    shiro实战系列(三)之架构
    shiro实战系列(二)之入门实战续
    ShopNC B2B2C多用户商城2014商业版,带微商城
    开源 SHOPNC B2B2C结算营运版 wap IM客服 API 手机app 短信通知
    PHP5.3下加速器ZendGuardLoader安装 (LNMP/lnmpa)
    XAMPP + Xdebug+Zend Studio
    magento 12 配置安装教程
    C#控件一览表
    GetXamarin.xambe
    新建电子监控点与测速点
  • 原文地址:https://www.cnblogs.com/xudy/p/6725255.html
Copyright © 2020-2023  润新知