💪💪💪细数数组的各种方法

177 阅读4分钟

Array对象

push(), pop()

push:用于在数组的末端添加一个或多个元素,并返回添加新元素后的数组长度。(这个方法会改变原数组)。

const arr = []

arr.push(1) // 1

arr.push('a') // 2

arr.push(true, {}) // 4

arr // [1, 'a', true, {}]

pop:从数组中删除最后一个元素,并返回该元素的值。(这个方法会更改数组的长度)。

const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']

plants.pop() // 'tomato'

plants // ['broccoli', 'cauliflower', 'cabbage', 'kale']

plants.pop()

plants // ['broccoli', 'cauliflower', 'cabbage']

shift(), unshift()

shift方法用于删除数组的第一个元素,并返回该元素。注意,该方法会改变原数组。

const a = ['a', 'b', 'c']

a.shift() // 'a'

a // ['b', 'c']

join()

join方法以指定参数作为分隔符,将所有数组成员连接为一个字符串返回。(不提供参数,默认用逗号分隔)

const a = [1, 2, 3, 4]

a.join(' ') // '1 2 3 4'

a.join(' | ') // '1 | 2 | 3 | 4'

a.join() // '1,2,3,4'

concat()

concat: 用于多个数组的合并。将新数组的成员,添加到原数组成员的后面,然后返回一个新数组,原数组不变。

['hello'].concat(['world']) // ['hello', 'world']

['hello'].concat(['world'], ['!']) // ['hello', 'world', '!']

[].concat({a: 1}, {b: 2}) // [{ a: 1 }, { b: 2 }]

[2].concat({a: 1}) // [2, { a: 1 }]

reverse()

reverse: 用于颠倒数组元素, 返回改变后的数组。(这个方法将改变数组)

const a = ['a', 'b', 'c']

a.reverse() // ['c', 'b', 'a']

a // ['c', 'b', 'a']

slice()

slice: 用于提取目标数组的一部分,返回一个新数组。(原数组不变)

slice(start, end)
第一个参数为起始位置(从 0 开始)
第二个参数为终止位置(但这个index的元素本身不包括在内)。

第二个不写的话,就从start到原数组的最后一个成员。

const a = ['a', 'b', 'c']

a.slice(0) // ['a', 'b', 'c']
a.slice(1) // ['b', 'c']
a.slice(1, 2) // ['b']
a.slice(2, 6) // ['c']
a.slice() // ['a', 'b', 'c']

splice()

splice: 用来删除原数组的一部分成员,并可以在删除的位置添加新的数组成员,返回值是被删除的元素。(会改变原数组)

arr.splice(start, count, addElement1, addElement2, ...)
const months = ['Jan', 'March', 'April', 'June']

months.splice(1, 0, 'Feb')
months // ['Jan', 'Feb', 'March', 'April', 'June']

months.splice(4, 1, 'May')
months // ['Jan', 'Feb', 'March', 'April', 'May']

sort()

sort: 将数组成员进行排序,默认是按照字典顺序排序。(排序后,原数组将被改变)

['d', 'c', 'b', 'a'].sort() // ['a', 'b', 'c', 'd']

[4, 3, 2, 1].sort() // [1, 2, 3, 4]

[11, 101].sort() // [101, 11]

[10111, 1101, 111].sort() // [10111, 1101, 111]

map()

循环数组,返回一个新数组,(不改变原数组)

const numbers = [1, 2, 3]

numbers.map(function (n) {
    reeturn n + 1
}) // [2, 3, 4]

numbers // [1, 2, 3]

filter()

filter: 用于过滤数组的各个元素,满足条件的元素组成一个新数组返回。

[1, 2, 3, 4, 5].filter(e => e > 3) // [4, 5]

some(), every()

const arr = [1, 2, 3, 4, 5]

// 参数: 元素,下标, 整个数组
// 如果数组arr有一个成员大于3,就返回true
arr.some((ele, index, arr) => ele > 3) // true 有找到符合要求的就返回true,否则就false

every()

跟 some 不同的是,some只一个符合要求即可,every是每个都要求符合才可。

const arr = [1, 2, 3, 4, 5]

arr.every((ele, index, arr) => ele > 3) // false

空数组

  • 空数组some返回false
  • 空数组every返回true
  • 回调函数都不会执行
[].some(v => v % 2 === 0) // false
[].every(v => v % 2 === 0) // true

indexOf(), lastIndexOf()

找到就返回这个元素在数组第一次出现的位置,没有就返回-1

const a = ['a', 'b', 'c']

a.indexOf('b') // 1

![image.png](https://p3-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/1670335b923b46da920793e030bf7ba8~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55uP54Gv:q75.awebp?rk3s=f64ab15b&x-expires=1771665778&x-signature=znvfTRgqXVFlgnpQo%2BrMm0xJMl0%3D)
a.indexOf('y') // -1

indexOf('元素', '搜索的开始位置')

['a', 'b', 'c'].indexOf('a', 1) // -1

lastIndexOf: 方法如其名,返回数组中给定元素最后一次出现的索引,如果不存在就返回-1.

lastIndexOf(searchElement, fromIndex):

fromIndex: 开始从fromIndex向前搜索数组。

at()

at: 找这个元素在数组哪,也就是index下标。

const a = [5, 12, 8, 130, 44]

a.at(2) // 8

a.at(0) // 5

a.at(-1) // 44

a.at(-2) // 130

copyWithin()

copyWithin: 浅复制 数组的一部分 到 同一数组中 的 另一个位置,并返回它。(会改变原数组,不会改变原数组的长度(也就是说copyWithin()永远不会扩展数组))。

  • copyWithin(target) // target是要替换的目标位置,从0为起始下标
    • target为负数的从末尾开始数
    • target < -array.length,则使用0
    • target >= array.length, 则不会拷贝任何内容
    • target > start, 则复制只会持续到array.length结束。(不会)

例子:target > start, 则复制只会持续到array.length结束。(不会)

image.png

  • copyWithin(target, start) // target是要替换的目标位置,start是要复制的元素序列的起始位置。

    • start < 0,
  • copyWithin(target, start, end) // target是要替换的目标位置,start是要复制的元素序列的起始位置, end是要复制的元素序列的结束位置。

const arr = ['a', 'b', 'c', 'd', 'e']

arr.copyWithin(0, 3, 4) // ['d', 'b', 'c', 'd', 'e']

arr.copyWithin(1, 3) // ['d', 'd', 'e', 'd', 'e']

flat()

flat:创建一个新的数组

const arr = [0, 1, 2, [3, 4]]

arr.flat() // [0, 1, 2, 3, 4]

手撕flat

Array.prototype.myFlat(arr, depth = 1) {
    
    return arr.reduce((result, item) => {
        if (Array.isArray(item)) {
            result = result.concat(flat(arr[i]));
        } else {
            result.push(arr[i]);
        }
    }, [])
    
}