JavaScript--數(shù)組和字符串常用方法

數(shù)組高級(jí)API

  • 遍歷數(shù)組的幾種方法
    • 利用傳統(tǒng)循環(huán)來遍歷數(shù)組
    for(let i = 0; i < arr.length; i++){
                 console.log(arr[i]);
             }
    
    • 利用for in循環(huán)來遍歷數(shù)組
             // 注意點(diǎn): 在企業(yè)開發(fā)中不推薦使用forin循環(huán)來遍歷數(shù)組
    for(let key in arr){
                console.log(key);
                console.log(arr[key]);
    }
    
    • 利用ES6中推出的for of循環(huán)來遍歷數(shù)組
    for(let value of arr){
                console.log(value);
    }
    
    • 還可以利用Array對(duì)象的forEach方法來遍歷數(shù)組
    forEach方法會(huì)自動(dòng)調(diào)用傳入的函數(shù)
          // 每次調(diào)用都會(huì)將當(dāng)前遍歷到的元素和當(dāng)前遍歷到的索引和當(dāng)前被遍歷的數(shù)組傳  遞給這個(gè)函數(shù)
          arr.forEach(function (currentValue, currentIndex, currentArray) {
              // console.log(currentValue, currentIndex, currentArray);
              console.log(currentValue);
    });
    
  • 數(shù)組的查找方法
    • findIndex方法
    findIndex方法: 定制版的indexOf, 找到返回索引, 找不到返回-1
          let index = arr.findIndex(function (currentValue, currentIndex, currentArray) {
              // console.log(currentValue, currentIndex, currentArray);
              // if(currentValue === 6){
              if(currentValue === 10){
                  return true;
              }
          });
          console.log(index);
    
    • find方法
    find方法如果找到了就返回找到的元素, 如果找不到就返回undefined
          let value = arr.find(function (currentValue, currentIndex, currentArray) {
              // console.log(currentValue, currentIndex, currentArray);
              // if(currentValue === 6){
              if(currentValue === 10){
                  return true;
              }
          });
          console.log(value);
    

數(shù)組的添加方法

  • filter方法
將滿足條件的元素添加到一個(gè)新的數(shù)組中
      let newArray = arr.filter(function (currentValue, currentIndex, currentArray) {
          // console.log(currentValue, currentIndex, currentArray);
          if(currentValue % 2 === 0){
              return true;
          }
      });
      console.log(newArray); // [2, 4]
  • map方法
將滿足條件的元素映射到一個(gè)新的數(shù)組中
      let newArray = arr.map(function (currentValue, currentIndex, currentArray) {
          // console.log(currentValue, currentIndex, currentArray);
          if(currentValue % 2 === 0){
              return currentValue;
          }
      });
      console.log(newArray); // [undefined, 2, undefined, 4, undefined]
  • 數(shù)組的排序方法
  let arr = ["c", "a", "b"];
        arr.sort();
        /*
        如果 compareFunction(a, b) 小于 0 ,那么 a 會(huì)被排列到 b 之前;
        如果 compareFunction(a, b) 等于 0 , a 和 b 的相對(duì)位置不變。
        如果 compareFunction(a, b) 大于 0 , b 會(huì)被排列到 a 之前
        注意點(diǎn): 如果元素是字符串類型, 那么比較的是字符串的Unicode編碼
        */
        arr.sort(function (a, b) {
            if(a > b){
                return -1;
            }else if(a < b){
                return 1;
            }else{
                return 0;
            }
        });
        console.log(arr);
  • 規(guī)律
    • 如果數(shù)組中的元素是數(shù)值類型
    • 如果需要升序排序, 那么就返回a - b;
    • 如果需要降序排序, 那么就返回b - a;

字符串常用方法

  • 獲取字符串長度
let str = "abcd";
console.log(str.length);
  • 獲取某個(gè)字符 [索引] / charAt
let str = "abcd";
let ch = str[1];
let ch = str.charAt(1);
console.log(ch);
  • 字符串查找 indexOf / lastIndexOf / includes
let str = "vavcd";
let index = str.indexOf("v");
let index = str.lastIndexOf("v");
console.log(index);
let result = str.includes("p");
console.log(result);
  • 拼接字符串 concat / +
let str1 = "www";
let str2 = "it666";
let str = str1 + str2; // 推薦
let str = str1.concat(str2);
console.log(str);
  • 截取子串 slice / substring / substr
let str = "abcdef";
let subStr = str.slice(1, 3);
let subStr = str.substring(1, 3);
let subStr = str.substr(1, 3);
console.log(subStr);
  • 字符串切割
let arr = [1, 3, 5];
let str = arr.join("-");
console.log(str);
let str = "1-3-5";
let arr = str.split("-");
console.log(arr);
  • 判斷是否以指定字符串開頭 ES6
let str = "http://www.it666.com";
let result = str.startsWith("www");
console.log(result);
  • 判斷是否以指定字符串結(jié)尾 ES6
let str = "lnj.jpg";
let result = str.endsWith("png");
console.log(result);
  • 字符串模板 ES6
let name = "lnj";
let age = 34;
// let str = "我的名字是" + name + ",我的年齡是" + age;
let str = `我的名字是${name},我的年齡是${age}`;
console.log(str);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容