es6常用的方法


一、判断是否包含,当包含时返回true,不包含时返回false,代码实例如下:     
    
    let string = "123345";
     let substring1 = "12";
        let substring2 = "01";
        string.includes(substring1); //true
        string.includes(substring2); //false
二、数组去重
 1、
  let array = [1, 1, 1, 1, 2, 3, 4, 4, 5, 3];
  let arr = new Set(array);
   console.log(arr);[1, 2, 3, 4, 5]
 2、
  let array = Array.from(new Set([1, 1, 1, 2, 3, 2, 4])); [1, 2, 3, 4]
 3、
  let gcs = [1,2,3,4,5]
  let ids = [1,3,4]
  const _arr2Set = new Set(ids)
   gcs = gcs.filter(item => !_arr2Set.has(item));  [2,5]
 三、对象
   hasOwnProperty()方法判断一个对象(list)中是否有某个(en)属性,如果有返回true,否则为false:
list.hasOwnProperty("en")
四、字符串

  1、三个方法

  includes():   返回布尔值,表示是否找到了参数字符串

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

  endsWith():返回布尔值 ,表示字符串是否在源字符串的头部

var s = "helllo world";
s.starsWith("hello")//true
s.endsWith("rld")//true
s.includes("o")//true
/*第二个参数表示开始搜索的位置*/
var s = 'Hello world';
s.startsWith('world', 6) // true
s.endsWith('Hello', 5) // true  第二个参数代表截取到前5 个
s.includes('Hello', 6) // false
2、遍历字符串
for(let item of 'str'){ console.log(item); }
3、补全字符串
'123'.padStart(5, '0')//00123 - 字符串不足5位,在头部补充不足长度的目标字符串
'345'.padEnd(5, '0')//34500 - 在尾部进行字符串补全


下次再写


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM