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