zoukankan      html  css  js  c++  java
  • es6新增方法---实用

    字符串相关:

    1、模板字符串:

    第一个用途,基本的字符串格式化。将表达式嵌入字符串中进行拼接。用${}来界定

    let name = "小明";

    let age = 20;

    console.log(`你好,${name},你今年是${age},岁吗?`)

    第二个用途,在ES5时我们通过反斜杠()来做多行字符串或者字符串一行行拼接。ES6反引号(``)直接搞定。

    2、字符串的几个方法:

    srt.includes() 判断是否包含某个值,返回值为布尔值

    str.repeat() 字符串重复几次,()里的值为次数

    str.startsWith() 判断字符串是否以()里的值开始,返回值为布尔值

    str.endsWith() 判断字符串是否以()的值结束,返回值为布尔值

    数组相关:

    1、map()

    [ ].map(function(item, index, array){

      return  item * 10;

    })

    对数组中的每一项执行一次回调函数,参数依次为:数组中的每一项、每一项的下标、原数组。map方法会改变原数组。

    2.filter()

    [ ].filter(function(item, index, array){

      return  item >= 1;

    })

    对数组中的每一项执行一次回调函数,参数依次为:数组中的每一项、每一项的下标、原数组。filter方法会新建一个数组,不会改变原数组

    3.some()

    var  result = [ ].some(function(item, index, array) {

      return  (item >= 2) 

    })

    判断数组中是否有大于等于2的项,只要有一个满足条件即返回true

    4.every()

    var  result = [ ].some(function(item, index, array) {

      return  (item >= 2) 

    })

     判断数组中的每一项是否大于等于2,只要有一个不满足条件即返回false

    5.lastIndexOf()

    var  index = [2,1,3,5,2 ].lastIndexOf('2'); //index=4

    返回给定项在数组中最后一次出现的位置

    6.reduce()

    var  sum = [ 2,9,2,4].reduce(function(result, current, index, array) {

      return   result + current; //sum=17

    })

    对数组元素按照给定的规则累积运算,result为每次得到的结果,current为当前值

    7.of()

      Array.of(3,7,8)   //输出[3,7,8]

    8.from()  将类数组转化为数组

      常用去重 Array.from(new Set([2,2,3,6]))  //输出[2,3,6]

    9.fill()

      [2,2,2].fill(11, 1)    //输出[2,11,11]

      第一个参数为填充的字符,第二个参数为起始填充下标

    10.find()  用于找出第一个符合条件的数组成员,没有符合条件的成员则返回undefined

      ["a","2","b"].find((item)=> item == "2")

      接收一个回调函数,数组的每一项都会执行该回调,参数依次为当前项、当前项下标、原数组



  • 相关阅读:
    mac上虚拟机:VMWare Fusion, VirtualBox, Parallels Desktop, CrossOver, Veertu
    linux使用其它用户 su
    CentOS7 rc.local开机开法启动
    taskkill
    Make sure that the controller has a parameterless public constructor.
    An Autofac Lifetime Primer
    Controlling Scope and Lifetime
    Instance scope
    Linq的TakeWhile的用法
    Git does not apply deleted files when merging an old branch into the master. How can I tell Git to apply deleted files?
  • 原文地址:https://www.cnblogs.com/yinxiaohua/p/9592440.html
Copyright © 2011-2022 走看看