开发中常用的数组字符窜的一些方法总结

  mounted(){
    let arr =['james','wade','andonni','bosh']
    let a =['james','wade','andonni','bosh']
    
    // console.log(arr.includes('andonni')); //true inclues判断是否存在
    // console.log(arr.join('黄金一代')); //james黄金一代wade黄金一代andonni黄金一代bosh   join一般用于向后台传递数据的时候
    // splice(index必需,howmany必需,item1,.....,itemX可选) 方法向/从数组中添加/删除项目,然后返回被删除的项目。index开始位置 howmany 为0不删除新增 其他数字删除,改变原始数组
    // console.log(arr.splice(1,0,'kewoer'));//注意区分 字符串没得这个splice只有slice  数组有slice和splice 
    // console.log(arr); // ["james", "kewoer", "wade", "andonni", "bosh"]
    // slice返回一个新的数组,包含从 start 到 end (不包括该元素)
       console.log(a.slice(0,3)); // ["james", "wade", "andonni"]
      // es6新增find findindex方法找到数组指定元素的位置
      // findIndex()方法返回数组中满足 提供的测试函数 的第一个元素的索引。否则返回-1。
      console.log(a.findIndex(item =>{
        item ==='bosh'
      }),'++++++');//3 如有返回下标 没有就是-1  有了下标自己该做什么就做什么

      
    let str='nbachapimcleavesKing james'
    console.log(str.includes('james'));// true
    console.log(str.endsWith('s'));// true
    console.log(str.indexOf('james'))//获取某个值的下标
    // string.slice(start, end)提取一个字符串 string.substring(start, end)提取一个字符串,end不支持负数效果差不多 string.substr(start, len)提取一个长度为len的字符串
    console.log(str.slice(5,str.indexOf('james')))//截取开始位置到结束位置的值 apimcleavesKing 
  }

  

 满足大部分开发中的需求




猜你喜欢

转载自www.cnblogs.com/myfirstboke/p/11204174.html