LOADING...

加载过慢请开启缓存(浏览器默认开启)

loading

Js数组相关

2022/11/30 js

1.splice() 方法

splice() 方法用于添加或删除数组中的元素。这种方法会改变原始数组。

array.splice(index,howmany,item1,.....,itemX):

参数 描述
index 必需。规定从何处添加/删除元素。该参数是开始插入和(或)删除的数组元素的下标,必须是数字。
howmany 可选。规定应该删除多少元素。必须是数字,但可以是 “0”。如果未规定此参数,则删除从 index 开始到原数组结尾的所有元素。
item1, …, itemX 可选。要添加到数组的新元素

如果从 arrayObject 中删除了元素,则返回的是含有被删除的元素的数组。

1.从指定位置删除元素

    //从第三个位置开始删除两个元素
    let arr = [1, 2, 3, 4]
    console.log(arr.splice(2, 2))// [1, 2]
    
    let arr = [1, 2, 3, 4]
    console.log(arr.splice(2, 0))// [1, 2, 3, 4]

2.添加元素

        let arr = [1, 2, 3, 4]
        arr.splice(2, 0, 9, 8, 6)
        console.log(arr)//[1, 2, 9, 8, 6, 3, 4]

3.替换元素

        let arr = [1, 2, 3, 4]
        arr.splice(1, 2, 9, 8)
        console.log(arr)//[1, 9, 8, 4]

2.slice:截取

slice(start,end):这种方法不会改变原始数组。

start:start:开始位置的索引

end:结束位置的索引(但不包含该索引位置的元素)

        let arr = [1, 2, 3, 4]
        let arr2 = arr.slice(0, 2)
        console.log(arr, arr2)//[1, 2, 3, 4] , [1, 2]
        
        let str = 'hello world'
        let str1 = str.slice(0, -2)
        console.log(str1)//hello wor

3.join():将数组元素转为字符串

array.join(separator)

参数 描述
separator 可选。指定要使用的分隔符。如果省略该参数,则使用逗号作为分隔符。

返回一个字符串。该字符串是通过把 arrayObject 的每个元素转换为字符串,然后把这些字符串连接起来,在两个元素之间插入 separator 字符串而生成的。

        let arr = [1, 5, 6, 8, 7]
        let arr1 = arr.join()
        console.log(arr1)//1,5,6,8,7
        
        let arr = [1, 5, 6, 8, 7]
        let arr1 = arr.join('-')
        console.log(arr1)//1-5-6-8-7

4.toString(): 将数组转换为字符串

array.toString()

返回数组的所有值用逗号隔开的字符串

        let arr = [1, 5, 6, 8, 7]
        let arr1 = arr.toString()
        console.log(arr1)//1,5,6,8,7

5.sort:数组排序

array.sort(sortfunction):

sort() 方法用于对数组的元素进行排序。

1.排序顺序可以是字母或数字,并按升序或降序。

2.默认排序顺序为按字母升序。

3.使用数字排序,你必须通过一个函数作为参数来调用。函数指定数字是按照升序还是降序排列。

参数 描述
sortfunction 可选。规定排序顺序。必须是函数。
        //字母顺序
        let fruits = ['Banana', 'Orange', 'Apple', 'Mango']
        fruits.sort()
        console.log(fruits)// ['Apple', 'Banana', 'Mango', 'Orange']
        
        //字母逆序
        let fruits = ['Banana', 'Orange', 'Apple', 'Mango']
        fruits.sort()
        fruits.reverse()
        console.log(fruits)
        let arr = [1, 3, 2, 6, 4, 7]
        arr.sort()
        console.log(arr)// [1, 2, 3, 4, 6, 7]
        //数字顺序
        let arr = [1, 3, 2, 6, 4, 7]
        arr.sort((a, b) => a - b)
        console.log(arr)// [1, 2, 3, 4, 6, 7]
        //数字逆序
        let arr = [1, 3, 2, 6, 4, 7]
        arr.sort((a, b) => b - a)
        console.log(arr)//[7, 6, 4, 3, 2, 1]       

6.reverse() :颠倒数组中元素的顺序

array.reverse()

返回颠倒顺序后的数组

        let arr = [1, 5, 6, 8, 7]
        arr.reverse()
        console.log(arr)//[7, 8, 6, 5, 1]

7.concat() :于连接两个或多个数组。

array1.concat(array2,array3,…,arrayX)

参数 描述
array2, array3, …, arrayX 必需。该参数可以是具体的值,也可以是数组对象。可以是任意多个。

返回一个新的数组。该数组是通过把所有 arrayX 参数添加到 arrayObject 中生成的。如果要进行 concat() 操作的参数是数组,那么添加的是数组中的元素,而不是数组。

        let arr = [1, 5, 6, 8, 7]
        let arr1 = [2, 3, 6]
        let arr2 = [3, 5, 9]
        let arr3 = arr.concat(arr1, arr2)
        console.log(arr3)//[1, 5, 6, 8, 7, 2, 3, 6, 3, 5, 9]

8.every:用于检测数组所有元素是否都符合指定条件

array.every(function(currentValue,index,arr), thisValue)
参数 描述
function(currentValue, index,arr) 必须。函数,数组中的每个元素都会执行这个函数函数参数:
currentValue 必须。当前元素的值
index 可选。当前元素的索引值
arr 可选。当前元素属于的数组对象
thisValue 可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。如果省略了 thisValue ,”this” 的值为 “undefined”

返回布尔值。如果所有元素都通过检测返回 true,否则返回 false。

every() 方法使用指定函数检测数组中的所有元素:

  • 如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
  • 如果所有元素都满足条件,则返回 true。

注意: every() 不会对空数组进行检测。

注意: every() 不会改变原始数组。

        let arr = [2, 8, 14, 9, 25, 6, 32]
        let flag = arr.every((item) => {
          return item > 10
        })
        console.log(flag)//false
        
        let arr = [2, 8, 14, 9, 25, 6, 32]
        let flag = arr.every((item) => {
          return item < 40
        })
        console.log(flag)//true

9.some:用于检测数组中的元素是否满足指定条件

array.some(function(currentValue,index,arr),thisValue)
参数 描述
function(currentValue, index,arr) 必须。函数,数组中的每个元素都会执行这个函数函数参数:
currentValue 必须。当前元素的值
index 可选。当前元素的索引值
arr 可选。当前元素属于的数组对象
thisValue 可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。如果省略了 thisValue ,”this” 的值为 “undefined”

返回布尔值。如果数组中有元素满足条件返回 true,否则返回 false。

some() 方法会依次执行数组的每个元素:

  • 如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测。
  • 如果没有满足条件的元素,则返回false。

注意: some() 不会对空数组进行检测。

注意: some() 不会改变原始数组。

        let arr = [2, 8, 14, 9, 25, 6, 32]
        let flag = arr.some((item) => {
          return item > 10
        })
        console.log(flag)//true
        
        let arr = [2, 8, 14, 9, 25, 6, 32]
        let flag = arr.some((item) => {
          return item > 40
        })
        console.log(flag)//false

10.shift:用于把数组的第一个元素从其中删除,并返回第一个元素的值。

此方法改变数组的长度!
array.shift()
        let arr = [2, 8, 14, 9, 25, 6, 32]
        arr.shift()
        console.log(arr)// [8, 14, 9, 25, 6, 32]

11.unshift() :向数组的开头添加一个或更多元素,并返回新的长度。

array.unshift(item1,item2, ..., itemX)
        let arr = [14, 9, 25, 6, 32]
        arr.unshift(2, 4, 5)
        console.log(arr)//[2, 4, 5, 14, 9, 25, 6, 32]

12.push:向数组的末尾添加一个或多个元素,并返回新的长度。

array.push(item1, item2, ..., itemX)
        let arr = [14, 9, 25, 6, 32]
        arr.push(2, 4, 5)
        console.log(arr)//[14, 9, 25, 6, 32, 2, 4, 5]

13.pop:用于删除数组的最后一个元素并返回删除的元素。

array.pop()
        let arr = [14, 9, 25, 6, 32]
        arr.pop()
        console.log(arr)//[14, 9, 25, 6]

14.map:返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

array.map(function(currentValue,index,arr), thisValue)
参数 描述
function(currentValue, index,arr) 必须。函数,数组中的每个元素都会执行这个函数函数参数:
currentValue 必须。当前元素的值
index 可选。当前元素的索引值
arr 可选。当前元素属于的数组对象
thisValue 可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。如果省略了 thisValue ,”this” 的值为 “undefined”
        let arr = [14, 9, 25, 6, 32]
        let arr1 = []
        arr.map((item) => {
          item = item + 1
          arr1.push(item)
        })
        console.log(arr, arr1)//[14, 9, 25, 6, 32]  [15, 10, 26, 7, 33]

15.filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

注意: filter() 不会对空数组进行检测。

注意: filter() 不会改变原始数组。

array.filter(function(currentValue,index,arr), thisValue)
参数 描述
function(currentValue, index,arr) 必须。函数,数组中的每个元素都会执行这个函数函数参数:
currentValue 必须。当前元素的值
index 可选。当前元素的索引值
arr 可选。当前元素属于的数组对象
thisValue 可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。如果省略了 thisValue ,”this” 的值为 “undefined”
        let arr = [14, 9, 25, 6, 32]
        let arr1 = arr.filter((item) => {
          return item > 20
        })
        console.log(arr, arr1)//[14, 9, 25, 6, 32] (2) [25, 32]

16.find:返回通过测试(函数内判断)的数组的第一个元素的值。

find() 方法为数组中的每个元素都调用一次函数执行:

  • 当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。
  • 如果没有符合条件的元素返回 undefined

注意: find() 对于空数组,函数是不会执行的。

注意: find() 并没有改变数组的原始值。

array.find(function(currentValue, index, arr),thisValue)
       //返回通过测试(函数内判断)的数组的第一个元素的值。之后的值不会再调用执行函数。
       let arr = [14, 9, 25, 6, 32]
        let arr1 = arr.find((item) => {
          return item > 20
        })
        console.log(arr, arr1)//[14, 9, 25, 6, 32] 25
        
        //没有符合条件的元素返回 undefined
        let arr = [14, 9, 25, 6, 32]
        let arr1 = arr.find((item) => {
          return item > 40
        })
        console.log(arr, arr1)//[14, 9, 25, 6, 32] undefined

17.findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。

findIndex() 方法为数组中的每个元素都调用一次函数执行:

  • 当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。
  • 如果没有符合条件的元素返回 -1

注意: findIndex() 对于空数组,函数是不会执行的。

注意: findIndex() 并没有改变数组的原始值。

array.findIndex(function(currentValue, index, arr), thisValue)
        //返回符合条件的数组第一个元素位置。之后的值不会再调用执行函数。
        let arr = [14, 9, 25, 6, 32]
        let index = arr.findIndex((item) => {
          return item > 20
        })
        console.log(arr, index)//[14, 9, 25, 6, 32] 2
        
        //如果没有符合条件的元素返回 -1
        let arr = [14, 9, 25, 6, 32]
        let index = arr.findIndex((item) => {
          return item > 40
        })
        console.log(arr, index)//[14, 9, 25, 6, 32] -1

18.isArray() 方法用于判断一个对象是否为数组。

如果对象是数组返回 true,否则返回 false。

Array.isArray(obj)
        let arr = [14, 9, 25, 6, 32]
        let obj = {
          name: '张三',
          sex: '男',
        }
        let num = 18
        let x = Array.isArray(arr)
        let y = Array.isArray(obj)
        let z = Array.isArray(num)
        console.log(x, y, z)//true false false

19.indexOf() 方法可返回数组中某个指定的元素位置。

该方法将从头到尾地检索数组,看它是否含有对应的元素。开始检索的位置在数组 start 处或数组的开头(没有指定 start 参数时)。如果找到一个 item,则返回 item 的第一次出现的位置。开始位置的索引为 0。

如果在数组中没找到指定元素则返回 -1。

array.indexOf(item,start)
参数 描述
item 必须。查找的元素。
start 可选的整数参数。规定在数组中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的首字符开始检索。

返回元素在数组中的位置,如果没有搜索到则返回 -1。

        let arr = [14, 9, 25, 6, 32]
        let arr1 = [
          {
            name: '张三',
            age: 18,
          },
          {
            name: '李四',
            age: 16,
          },
          {
            name: '王五',
            age: 21,
          },
        ]
        let index = arr.indexOf(9)
        //数组元素为对象时查找不了
        let index2 = arr1.indexOf({
          name: '李四',
          age: 16,
        })
        console.log(index, index2)//1 -1
        
        
        let arr = [14, 6, 9, 25, 6, 32]
        let index = arr.indexOf(6)
        console.log(index)//1

20.lastIndexOf() 方法可返回一个指定的元素在数组中最后出现的位置,从该字符串的后面向前查找。

如果要检索的元素没有出现,则该方法返回 -1。

该方法将从尾到头地检索数组中指定元素 item。开始检索的位置在数组的 start 处或数组的结尾(没有指定 start 参数时)。如果找到一个 item,则返回 item 从尾向前检索第一个次出现在数组的位置。数组的索引开始位置是从 0 开始的。

如果在数组中没找到指定元素则返回 -1。

array.lastIndexOf(item,start)
参数 描述
item 必需。规定需检索的字符串值。
start 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。

如果在 stringObject 中的 fromindex 位置之前存在 searchvalue,则返回的是出现的最后一个 searchvalue 的位置。

        let arr = [14, 6, 9, 25, 6, 32]
        let lastIndex = arr.lastIndexOf(6)
        console.log(lastIndex)//4

21.includes() 方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false。

arr.includes(searchElement)
arr.includes(searchElement, fromIndex)
参数 描述
searchElement 必须。需要查找的元素值。
fromIndex 可选。从该索引处开始查找 searchElement。如果为负值,则按升序从 array.length + fromIndex 的索引开始搜索。默认为 0。

如果找到指定值返回 true,否则返回 false。

        let arr = [14, 6, 9, 25, 6, 32]
        let flag = arr.includes(6)
        console.log(flag)//true
img_show