JavaScript-数组

数组

创建数组

  • 通过构造函数创建:let arr = new Array(length)
  • 通过字面量创建:let arr = [ ... ]

数组增删改查

    • unshift:头部插入
    • push:尾部插入
    • splice(stratIndex, 0, insertData):中间插入
    • shift:头部删除
    • pop:尾部删除
    • splice(stratIndex, deleteNumbers):中间删除
    • arr[index] = ...
    • splice(stratIndex, deleteNumbers, insertData)
    • arr[index]

数组常用方法

  • 清空数组

    • arr = []
    • arr.length = 0
    • arr.splice(0, arr.length)
  • 数组转字符串

    • arr.toString
    • arr.join( ... )
  • 字符串转数组

    str.split( ... )

  • 数组拼接

    • arr1.concat(arr2)
    • let res = [...arr1, ...arr2]
  • 翻转数组

    arr.reverse()

  • 数组截取

    • arr.slice(startIndex, endIndex)
    • arr.splice(startIndex, length)
  • 元素查找

    • arr.indexOf(startIndex, el)
    • arr.lastIndexOf(startIndex, el)
  • 判断元素是否存在

    • arr.indexOf() / arr.lastIndexOf()
    • arr.includes(el)
  • 替换数组中素有元素

    arr.fill( ... )

数组高级API

  • 排序

    // 对数组进行排序 
    arr.sort(function (a, b){
        return a - b; // 升序
    });
    
    arr.sort(function (a, b){
        return b - a; // 降序
    });
    
  • 遍历

    // for-in
    for(let index in arr){
    	...
    	// for-in 是遍历对象的方法,一般不用在数组中,因为对象不强调顺序,而数组强调顺序
    }
        
    // for-of
    for (let value of arr){
        // ...
    }
        
    // forEach
    arr.forEach(function (curValue, curIndex, curArr){
        // ...
    });
    
  • 查询

    // findIndex
    arr.findIndex(function(curValue, curIndex, curArr){
        if (conditions){
            return true;
        }
        
        // 存在会返回索引,不存在返回-1
    });
    
    // find
    arr.find(function (cueValue, curIndex, curArr){
        if (conditions){
            return true;
        }
        
        // 存在会返回存在的元素,不存在返回undefined
    })
    
  • 过滤

    // fliter
    let res = arr.filter(function (curValue, curIndex, curArr){
        if (conditions){
            return true;
        }
        
        // 会将满足条件的元素存放到一个新的数组中
    });
    
  • 映射

    // map
    let res = arr.map(function (curValue, curIndex, curArr){
      // ...
        
        // 创建一个与原数组长度一样的新数组,在将经过过滤、修改后的元素存放到新数组中
    });
    
    
  • 删除元素

    // splice
    arr.splice(startIndex, delLength);
    // splice 删除后的数据会从原数组中消失,并且原数组的长度会发生改变
    
    // delete
    delete arr[index];
    // delete 删除后的数据会变成空(empty)数据,并且原数组的长度不变
    
  • 元素检测

    // every
    arr.every(function (curValue, curIndex, curArr){
        return conditions
        
        // 判断数组中的数据是否都满足某条件
    });
    
    // some
    arr.some(function (curValue, curIndex, curArr){
        return conditions
        
        // 判断数组中的数据是否存在满足某条件的数据
    });
    
  • 数组判断

    arr instanceof Array
    
    Array.isArray(arr)
    
  • 返回自身

    arr.valueof()
    

字符串常用方法

由于在Javascript中字符串是一种特殊的数,所有大多数数组的方法也可以作用于字符串

  • 字符串长度

    str.length
    
  • 获取字符

    str[index]
    
    str.charAt(index)
    
  • 字符串查找

    str.indexOf(startIndex, ... )
    
    str.lastIndexOf(startIndex, ... )
    
  • 字符串切割

    str.slice(startIndex, endIndex)
    
    str.substring(startIndex, endIndex)
    
    str.substr(startIndex, length)
    
  • 字符串拼接

    str1.concat(str2)
    
  • 字符串切割为数组

    str.split( ... )
    
  • 是否以指定字符串开头 / 结尾

    str1.startsWith(str2)
    
    str1.endsWith(str2)
    
原文地址:https://www.cnblogs.com/luwenfeng/p/11693757.html