美文网首页
JavaScript--数组和字符串常用方法

JavaScript--数组和字符串常用方法

作者: 绚丽多彩的白 | 来源:发表于2020-11-17 14:35 被阅读0次

数组高级API

  • 遍历数组的几种方法
    • 利用传统循环来遍历数组
    for(let i = 0; i < arr.length; i++){
                 console.log(arr[i]);
             }
    
    • 利用for in循环来遍历数组
             // 注意点: 在企业开发中不推荐使用forin循环来遍历数组
    for(let key in arr){
                console.log(key);
                console.log(arr[key]);
    }
    
    • 利用ES6中推出的for of循环来遍历数组
    for(let value of arr){
                console.log(value);
    }
    
    • 还可以利用Array对象的forEach方法来遍历数组
    forEach方法会自动调用传入的函数
          // 每次调用都会将当前遍历到的元素和当前遍历到的索引和当前被遍历的数组传  递给这个函数
          arr.forEach(function (currentValue, currentIndex, currentArray) {
              // console.log(currentValue, currentIndex, currentArray);
              console.log(currentValue);
    });
    
  • 数组的查找方法
    • 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);
    

数组的添加方法

  • filter方法
将满足条件的元素添加到一个新的数组中
      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方法
将满足条件的元素映射到一个新的数组中
      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]
  • 数组的排序方法
  let arr = ["c", "a", "b"];
        arr.sort();
        /*
        如果 compareFunction(a, b) 小于 0 ,那么 a 会被排列到 b 之前;
        如果 compareFunction(a, b) 等于 0 , a 和 b 的相对位置不变。
        如果 compareFunction(a, b) 大于 0 , b 会被排列到 a 之前
        注意点: 如果元素是字符串类型, 那么比较的是字符串的Unicode编码
        */
        arr.sort(function (a, b) {
            if(a > b){
                return -1;
            }else if(a < b){
                return 1;
            }else{
                return 0;
            }
        });
        console.log(arr);
  • 规律
    • 如果数组中的元素是数值类型
    • 如果需要升序排序, 那么就返回a - b;
    • 如果需要降序排序, 那么就返回b - a;

字符串常用方法

  • 获取字符串长度
let str = "abcd";
console.log(str.length);
  • 获取某个字符 [索引] / 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);
  • 判断是否以指定字符串结尾 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);

相关文章

  • JavaScript--数组和字符串常用方法

    数组高级API 遍历数组的几种方法利用传统循环来遍历数组for(let i = 0; i < arr.length...

  • js中字符串和数组的常用方法

    字符串的常用方法 数组的常用方法

  • 字符串&数组方法

    介绍一些项目常用到的数组和字符串方法,这些方法方便我们灵活运用请求的数据中的数组或者字符串。 数组方法 字符串转换...

  • Java Script 常用数组方法(不包含数组迭代方法)

    一、数组常用方法 1、数组转为字符串 Array.toStriing() 示例: 2、数组转为字符串 Array....

  • js基础了解

    js数组常用遍历方法使用: js数组常用操作方法使用: 基本逻辑运算: 基本字符串操作方法:

  • js 常用方法总结

    字符串的常用属性,概览 Array对象的方法; 详细js数组常用方法大全

  • java字符串

    String常用方法 字符串和byte数组之间的相互转换 == 和equals方法的区别 StringBuilde...

  • Javascript中数组和字符串的常用方法

    Javascript中数组与字符串常用方法 数组常用方法 学习思路:这个方法干啥用?是否要传入参数?是否有返回值?...

  • 2018-12-02

    数组 数组常用方法 通过标签获取元素 循环语句 数组去重 字符串处理的方法 字符串反转 计算器 定时器弹框

  • 字符串、定时器、数组

    字符串处理的方法 字符串反转 定时器弹框 定时器使用方法 定时器动画 数组 数组的常用方法 数组去重 作业 for...

网友评论

      本文标题:JavaScript--数组和字符串常用方法

      本文链接:https://www.haomeiwen.com/subject/rwjfuktx.html