JavaScript数组的方法大全(最新)

  • JavaScript数组的方法大全(最新)已关闭评论
  • 79 次浏览
  • A+
所属分类:Web前端
摘要

趁着有时间,总结了下数组所有的属性和方法,记录博客,便于后续使用 


JavaScript数组方法大全

趁着有时间,总结了下数组所有的属性和方法,记录博客,便于后续使用

  • at()

    • at方法,用于获取数组中,对应索引位置的值,不能修改。
    • 语法:array.at(count);
    • 参数:count,数组的索引值
    • 返回值:返回该索引在数组中对应的值,如果count大于等于数组的长度时,返回undefined
    • const arr = [1,2,3,4,5,6,7,8,9,10]; const len = arr.at(1) console.log(len);// 2
  • concat()

    • concat方法用于连接两个或多个数组
    • 语法:array.concat(arr1,arr2,arr...);
    • 参数: arr1, ...arrx,可选。该参数可以是具体的值,也可以是数组对象。可以是任意多个。参数为空时,将拷贝调用该方法的数组作为副本返回。
    • 返回值:Array,返回一个新的数组。该数组是通过把所有 arrayX 参数添加到 arrayObject 中生成的。如果要进行 concat() 操作的参数是数组,那么添加的是数组中的元素,而不是数组。
    • const arr = [1,2,3,4,5,6,7,8,9,10]; const arr1 = arr.concat(); const arr2 = arr.concat(['我是lanny',12],['嘿嘿'],'啦啦啦'); console.log(arr1);//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; console.log(arr2);//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '我是lanny', 12, '嘿嘿', '啦啦啦'];
  • constructor

    • constructor属性,返回数组的构造函数,其返回值是对函数的引用,而不是函数的名称,对于 JavaScript 数组,constructor 属性返回function Array() { [native code] },对于 JavaScript 对象,constructor 属性返回:function Object() { [native  code] }
    • 可以通过如下形式,对数据的类型进行判断
    • const arr = [1,2,3]
    • console.log(arr.constructor === Array);// true
  • copyWithin

    • copyWithin用于从数组的指定位置拷贝元素到数组的另一个指定位置中。
    • 语法:array.copyWithin(target, start, end)
    • 参数:target-必需。复制到指定目标索引位置。start-可选。元素复制的起始位置。end-可选。停止复制的索引位置 (默认为 array.length)。如果为负值,表示倒数。
    • 注意,该方法会改变原数组
    • const arr = [1,2,3,4,5,6,7,8,9,10]; arr.copyWithin(0,4,9); console.log(arr);// [5, 6, 7, 8, 9, 6, 7, 8, 9, 10]
  • entries()

    • entries方法返回一个数组的迭代对象,该对象包含数组的键值对 (key/value)。迭代对象中数组的索引值作为 key, 数组元素作为 value.
    • 语法:array.entries()
    • 返回值:Array Iterator一个数组的迭代对象。
    • const arr = ['lanny','jhon','alex','emily'].entries(); for (const [key,value] of arr) {       console.log(key,value) } //0 'lanny' //1 'jhon' //2 'alex' //3 'emily'
  • every()

    • every方法用于检测数组所有元素是否都符合指定条件, 指定函数检测数组中的所有元素:如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。如果所有元素都满足条件,则返回 true。
    • 语法:array.every(function(currentValue, index,arr),thisValue);
    • 注意: every() 不会对空数组进行检测。every() 不会改变原始数组。
    • 参数:
      • function(currentValue, index,arr)
        • 必须。函数,数组中的每个元素都会执行这个函数。
        • currentValue:必须。每次遍历循环时,当前元素的值。
        • index-可选。当前元素的索引值。
        • arr-可选。当前元素属于的数组对象。
      • thisValue 可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined"。
    • 返回值:boolean,true或者false。
    • const arr = [1,2,3,4,5,6,7,8,9,10]; const condition = arr.every(function(currentValue,index,arr){      console.log(this);//arr      return typeof currentValue === 'number'  },arr)  console.log(condition) // true
  • fill()

    • fill方法用于将一个固定值替换数组的元素。
    • 语法:array.fill(value,start,end);
    • 参数:value-必需。填充的值。start-可选。开始填充位置。end-可选。停止填充位置 (默认为 array.length)。
    • 返回值:Array,返回新替换的值作为数组的唯一项。
    • 注意:该方法会改变原数组
    • const arr = [1,2,3,4,5,6,7,8,9,10]; const arr1 = arr.fill('嘟嘟',0,1); console.log(arr);//['嘟嘟', 2, 3, 4, 5, 6, 7, 8, 9, 10] console.log(arr1);//['嘟嘟']
  • filter()

    • filter方法用于过滤,返回指定函数中要求的条件,filter() 不会改变原始数组、不会对空数组进行检测。
    • 语法:array.filter(function(currentValue,index,arr), thisValue);
    • 参数:
      • function(currentValue, index,arr)
        • 必须。函数,数组中的每个元素都会执行这个函数。
        • currentValue:必须。每次遍历循环时,当前元素的值。
        • index-可选。当前元素的索引值。
        • arr-可选。当前元素属于的数组对象。
      • thisValue 可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined"。
    • 返回值:Array,返回一个新数组,满足所执行的函数条件
    • const arr = [1,2,3,"啦啦",6,7,"小L",9,10]; const arr1 = arr.filter((item)=>typeof item === 'string'); console.log(arr1);//["啦啦", "小L"]
  • find()

    • find方法返回通过测试(函数内判断)的数组的第一个元素的值。find() 方法为数组中的每个元素都调用一次函数执行:当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。如果没有符合条件的元素返回 undefined。注意: find() 对于空数组,函数是不会执行的、 find() 并没有改变数组的原始值。
    • 语法:array.find(function(currentValue,index,arr), thisValue);
    • 参数:
      • function(currentValue, index,arr)
        • 必须。函数,数组中的每个元素都会执行这个函数。
        • currentValue:必须。每次遍历循环时,当前元素的值。
        • index-可选。当前元素的索引值。
        • arr-可选。当前元素属于的数组对象。
      • thisValue 可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined"。
    • 返回值:any,任意类型,满足函数条件的第一个对应值,如果都不满足,返回undefined。
    • const arr = [1,2,3,4,5,"嘟嘟",7,"嘿嘿",9,10]; const value = arr.find((item)=>typeof item === 'number'));//2 console.log(value);// 1
  • findIndex()

    • findIndex方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。方法为数组中的每个元素都调用一次函数执行:当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。如果没有符合条件的元素返回 -1。注意: findIndex() 对于空数组,函数是不会执行的、findIndex() 并没有改变数组的原始值。
    • 语法:array.findIndex(function(currentValue, index, arr), thisValue);
    • 参数:
      • function(currentValue, index,arr)
        • 必须。函数,数组中的每个元素都会执行这个函数。
        • currentValue:必须。每次遍历循环时,当前元素的值。
        • index-可选。当前元素的索引值。
        • arr-可选。当前元素属于的数组对象。
      • thisValue 可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined"。
    • 返回值:number,索引值,返回符合条件的元素的索引位置,之后的值不会再调用执行函数。如果没有符合条件的元素返回 -1。
    • const arr = [1,2,3,4,5,6,7,8,9,10]; const index = arr.findIndex((item=>item === 9); console.log(index);//8  注意,是索引位置
  • findLast()

    • 该方法与find方法相同,唯一区别是,遍历调用函数查找时,是倒序查找,从数组末尾,向前查找的
    • const arr = [1,2,3,4,5,6,7,8,9,10]; const last = arr.findLast((item)=>typeof item === 'number'); console.log(last);//10
  • findLastIndex()

    • 该方法与findIndex方法相同,唯一区别是,遍历调用函数查找时,是倒序查找,从数组末尾,向前查找的.
    • const arr = [1,2,3,4,5,6,7,8,9,10]; const lastIndex = arr.findLastIndex((item)=>typeof item === 'number'); console.log(lastIndex);//9 注意,是索引位置
  • flat()

    • 该方法用于将嵌套的多维数组,转换成一维数组,该方法会自动删除,空的索引值,且不会改变原数组。
    • 语法:array.flat(hierarchy)
    • 参数值:hierarchy-数组的嵌套层级,number,或者 Infinity-不管嵌套多少层
    • 返回值:Array,转换之后的一维数组。
    • const arr = ['a',,[0,5,[18,29],['嘿嘿',{key:'002'}]]]; const arr1 = arr.flat(Infinity); console.log(arr1);//['a', 0, 5, 18, 29, '嘿嘿', {key:'002'}]
  • flatMap()

    • 先对数组中每个元素进行处理,再对数组执行 flat() 方法。
    • 语法:array.flatMap(function(currentValue,index,arr),thisValue);
    • 参数:
      • function(currentValue, index,arr)
        • 必须。函数,数组中的每个元素都会执行这个函数。
        • currentValue:必须。每次遍历循环时,当前元素的值。
        • index-可选。当前元素的索引值。
        • arr-可选。当前元素属于的数组对象。
      • thisValue 可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined"。
    • 返回值:Array,返回处理之后的数组。
    • const arr = ['a',,[0,5,[18,29],['嘿嘿',{key:'002'}]]] const arrFlat = arr.flatMap(item=>{     if(typeof item === 'string'){         return item + '已处理'     }else{         return item     } }) console.log(arrFlat);// ['a已处理', 0, 5, [18,29], ['嘿嘿',{key:'002']]
  • forEach()

    • forEach().方法用于调用数组的每个元素,并将元素传递给回调函数,即遍历数组,在函数中进行操作。forEach() 对于空数组是不会执行回调函数的。该方法没有返回值,即undefined
    • 语法:array.forEach(callbackFn(currentValue, index, arr), thisValue);
    • 参数:
      • function(currentValue, index,arr)
        • 必须。函数,数组中的每个元素都会执行这个函数。
        • currentValue:必须。每次遍历循环时,当前元素的值。
        • index-可选。当前元素的索引值。
        • arr-可选。当前元素属于的数组对象。
      • thisValue 可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined"。
    • 返回值:无,undefined。
    • const arr = [1,"嘟嘟",3,4,5,6,7,8,9,10]; arr.forEach((item,index)=>{     if(typeof item === 'number'){         arr[index] = item * 2     } }) console.log(arr);// [2, '嘟嘟', 6, 8, 10, 12, 14, 16, 18, 20]
  • indexOf()

    • indexOf方法可返回数组中某个指定的元素位置。该方法将从头到尾地检索数组,看它是否含有对应的元素。开始检索的位置在数组 start 处或数组的开头(没有指定 start 参数时)。如果找到一个 item,则返回 item 的第一次出现的位置。开始位置的索引为 0。如果在数组中没找到指定元素则返回 -1。
    • 语法:array.indexOf(item,start);
    • 参数:item必须。查找的元素。start可选的整数参数。规定在数组中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的首字符开始检索。
    • 返回值:number,索引值,元素在数组中的位置,如果没有搜索到则返回 -1。
      const arr = ["嘟嘟",2,3,4,5,6,7,8,9,10]; const index = arr.indexOf('嘟嘟') console.log(index);// 0

  • lastIndexOf()

    • lastIndexOf()方法可返回一个指定的元素在数组中最后出现的位置,从该字符串的后面向前查找。如果要检索的元素没有出现,则该方法返回 -1。该方法将从尾到头地检索数组中指定元素 item。开始检索的位置在数组的 start 处或数组的结尾(没有指定 start 参数时)。如果找到一个 item,则返回 item 从尾向前检索第一个次出现在数组的位置。数组的索引开始位置是从 0 开始的。如果在数组中没找到指定元素则返回 -1。
    • 语法:array.lastIndexOf(item,start);
    • 参数:item必需。规定需检索的字符串值。start可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。
    • 返回值:number,索引值,元素在数组中的位置,如果没有搜索到则返回 -1。
    • const arr = [0,1,2,4,1,5]; const index = arr.lastIndexOf(1); console.log(index);//4 索引位置,倒数
  • join()

    • join()方法用于把数组中的所有元素转换一个字符串。元素是通过指定的分隔符进行分隔的,该方法不会改变原数组。
    • array.join(separator);
    • 参数:separator可选。指定要使用的分隔符。如果省略该参数,则使用逗号作为分隔符。
    • 返回值:String返回一个字符串。该字符串是通过把 arrayObject 的每个元素转换为字符串,然后把这些字符串连接起来,在两个元素之间插入 separator 字符串而生成的。
    • const arr = [1,2,3,4,'嘟嘟',6,7,8,9,10]; const arr1 = arr.join(''); console.log(arr1);// "1234嘟嘟678910"
  • keys()

    • keys()方法用于从数组创建一个包含数组键的可迭代对象.如果对象是数组返回 true,否则返回 false。
    • 返回值:一个数组可迭代对象。

    • const arr = ['lanny','jhon','alex','emily']; for (const item of arr.keys()) {     console.log(item)// 数组的索引值,依次打印 0/1/2/3 }
    •  
    •  
    •  

     

  • includes()

    • includes()方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false
    • 语法:array.includes(searchElement, fromIndex);
    • 参数:searchElement必须。需要查找的元素值。fromIndex可选。从该索引处开始查找 searchElement。如果为负值,则按升序从 array.length + fromIndex 的索引开始搜索。默认为 0,如果fromIndex大于等于数组长度,则返回 false,该数组不会被搜索。
    • 返回值:布尔值。如果找到指定值返回 true,否则返回 false。
    • const arr = ['嘟嘟', 4, 6, 8, 10, 12, 14, 16, 18, 20]; console.log(arr.includes('嘟嘟'));// true