JS数组方法

Array.prototype.concat()

  • 用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组
  • 如果数组元素是引用型数据,则复制的是元素的浅拷贝,如果此数据被更改,则更改对于新旧数组都是可见的
    const arr1 = [1,2]
    const arr2 = [4,5]
    const arr3 = [6,7]
    const res1 = arr1.concat(arr2)// [1,2,4,5]
    const res2 = arr1.concat(arr2,arr3,10)// [1,2,4,5,6,7,10]
    复制代码

Array.prototype.every()

  • 测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。若是空数组,则都会返回trueevery()不会改变原数组
    const arr1 = [1,2,3,4,5]
    const res = arr1.every(val => val > 3) // false
    复制代码

Array.prototype.filter()

  • 返回一个新数组, 其包含通过所提供函数实现的测试的所有元素
    const words = [ 'elite', 'exuberant', 'destruction', 'present' ]
    const res = words.filter(word => word.length < 6) // [ 'elite' ]
    复制代码

Array.prototype.from()

  • 从一个类似数组或可迭代对象创建一个新的,浅拷贝的数组实例
    Array.from('foo') // [ "f", "o", "o" ]
    
    const set = new Set(['foo', 'bar', 'baz', 'foo'])
    Array.from(set) // [ "foo", "bar", "baz" ]
    
    const mapper = new Map([['1', 'a'], ['2', 'b']])
    Array.from(mapper.values()) // ['a', 'b']
    复制代码

Array.prototype.sort()

  • 用原地算法对数组的元素进行排序,改变并返回数组。默认排序顺序是在将元素转换为字符串,然后比较它们的UTF-16代码单元值序列时构建的
  • 参数:compareFunction(a, b)
    • 如果 compareFunction(a, b) 小于 0 ,那么 a 会被排列到 b 之前
    • 如果 compareFunction(a, b) 大于 0 , b 会被排列到 a 之前
    const array1 = [1, 30, 4, 21, 100000]
    array1.sort() // [1, 100000, 21, 30, 4]
    array1.sort((a, b) => a - b) // [1, 4, 21, 30, 100000]
    array1.sort((a, b) => b - a) // [100000, 30, 21, 4, 1]
    复制代码

Array.prototype.findIndex()

  • 返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1
    const arr = [5, 12, 8, 130, 44]
    const res = arr.findIndex(num => num > 13) // 3
    复制代码

Array.prototype.find()

  • 返回数组中满足提供的测试函数的第一个元素的值。若没有找到对应元素则返回undefined
    const arr = [5, 12, 8, 130, 44]
    const res = arr.findIndex(num => num > 13) // 130
    复制代码

Array.prototype.forEach()

  • 对数组的每个元素执行一次给定的函数,返回undefined,已删除或者未初始化的项将被跳过(例如在稀疏数组上)
    const arraySparse = [1,3,,7]
    let numCallbackRuns = 0
    arraySparse.forEach(function(element){
        console.log(element) // 1 3 7
        numCallbackRuns++ // 3
    })
    复制代码

Array.prototype.includes()

  • 判断一个数组是否包含一个指定的值,根据情况,如果包含则返回true,否则返回false
    const arr = [5, 12, 8, 130, 44]
    const res = arr.includes(5) // true
    复制代码

Array.prototype.indexOf()

  • 返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1
    const arr = [5, 12, 8, 130, 44]
    const res = arr.indexOf(8) // 2
    const res1 = arr.indexOf(2) // -1
    复制代码

Array.prototype.map()

  • 创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值
    const array1 = [1, 4, 9, 16]
    const map1 = array1.map(x => x * 2) // [2, 8, 18, 32]
    复制代码

Array.prototype.pop()

  • 从数组中删除最后一个元素,并返回该元素的值。此方法更改原数组的长度
    const plants = [1, 4, 9, 16]
    const res= plants.pop() // 16
    复制代码

Array.prototype.push()

  • 方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度
    const plants = [1, 4, 9, 16]
    const res= plants.push(4) // 5
    复制代码

Array.prototype.reverse()

  • 将数组中元素的位置颠倒,并返回该数组。数组的第一个元素会变成最后一个,数组的最后一个元素变成第一个。该方法会改变原数组
    const plants = [1, 4, 9]
    const res= plants.reverse() // [9, 4, 1]
    复制代码

Array.prototype.shift()

  • 从数组中删除第一个元素,并返回该元素的值。如果数组为空则返回undefined,此方法更改原数组的长度
    const plants = [1, 4, 9]
    const res= plants.shift() // 1
    复制代码

Array.prototype.unshift()

  • 将一个或多个元素添加到数组的开头,并返回该数组的新长度,该方法修改原有数组
    const plants = [1, 4, 9]
    const res= plants.unshift(3) // 4
    复制代码

Array.prototype.slice(begin, end)

  • 返回一个新的数组/数组对象,这是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变
  • 如果 end 被省略,则 slice 会一直提取到原数组末尾
  • 如果参数为负数, 则它表示在原数组中的倒数第几个元素结束抽取。 slice(-2,-1) 表示抽取了原数组中的倒数第二个元素到最后一个元素(不包含最后一个元素,也就是只有倒数第二个元素
    const plants = [1, 4, 9]
    const res= plants.slice(1) // [4, 9]
    复制代码

Array.prototype.splice(start, deleteCount, item)

  • 通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。
  • 参数
    • start:指定修改的开始位置(从0计数)。如果超出了数组的长度,则从数组末尾开始添加内容;如果是负值,则表示从数组末位开始的第几位(从-1计数,这意味着-n是倒数第n个元素并且等价于array.length-n);如果负数的绝对值大于数组的长度,则表示开始位置为第0位。
    • deleteCount: 整数,表示要移除的数组元素的个数。如果为0或者负数这种情况下,至少应该添加一个元素
    • item1, item2…:要添加进数组的元素,从start 位置开始。如果不指定,则 splice() 将只删除数组元素。
    const plants = [1, 4, 9]
    const res1 = plants.splice(1, 0, 1) // [1, 1, 4, 9] 新增
    const res2 = plants.splice(1, 1) // [1, 4, 9] 删除
    const res3 = plants.splice(1, 1, 3) // [1, 3, 9] 修改/替换
    复制代码

Array.prototype.some()

  • 测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值。
  • 如果用一个空数组进行测试,在任何情况下它返回的都是false。
    const plants = [1, 4, 9]
    const res= plants.some(1) // true
    复制代码

Array.prototype.reduce()

  • 对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值
  • 回调函数第一次执行时,accumulator 和currentValue的取值有两种情况:如果调用reduce()时提供了initialValue,accumulator取值为initialValue,currentValue取数组中的第一个值;如果没有提供 initialValue,那么accumulator取数组中的第一个值,currentValue取数组中的第二个值。
  • 参数:
    • callback函数:
      • accumulator 累计器
      • currentValue 当前值
      • currentIndex 当前索引
      • array 数组
    • initialValue:
        [0, 1, 2, 3, 4].reduce((accumulator, currentValue, currentIndex, array) => {  
            return accumulator + currentValue  
        }, initialValue)
    // 如果未提供initialValue;第1次-第5次执行结果分别为:1、3、6、10
    // 如果initialValue = 2;第1次-第5次执行结果分别为:2、5、8、12
    复制代码
  • 实用方法
    • 计算数组中每个元素出现的次数
        var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
        var countedNames = names.reduce(function (allNames, name) {
        if (name in allNames) {
          allNames[name]++;
        } else {
         allNames[name] = 1;
        }
        return allNames;
      }, {});
      // countedNames: { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
    复制代码

以上摘录自MDN

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享