对数组的操作
1.1 forEach()
语法:
arr.forEach(callback(currentValue, index, array), thisValue)参数:
callback:数组中每个元素需要调用的函数。currentValue:当前元素的值。index:当前元素的索引。array:被遍历的数组本身。thisValue:可选参数,传递给函数的值,该值被用作this。
返回值:
undefined描述:
forEach()方法对数组的每个元素执行一次给定的函数。示例:
jsconst arr = [1, 2, 3, 4, 5] arr.forEach((element, index, array) => { console.log(element, index, array) })
1.2 map()
语法:
arr.map(callback(currentValue, index, array), thisValue)参数:
callback:数组中每个元素需要调用的函数。currentValue:当前元素的值。index:当前元素的索引。array:被遍历的数组本身。thisValue:可选参数,传递给函数的值,该值被用作this。
返回值:一个新的数组,每个元素都是回调函数的结果。
描述:
map()方法创建一个新数组,其结果是该数组中的每个元素都是回调函数的结果。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.map((element, index, array) => { return element * 2 }) console.log(result) // [2, 4, 6, 8, 10]
1.3 filter()
语法:
arr.filter(callback(currentValue, index, array), thisValue)参数:
callback:数组中每个元素需要调用的函数。currentValue:当前元素的值。index:当前元素的索引。array:被遍历的数组本身。thisValue:可选参数,传递给函数的值,该值被用作this。
返回值:一个新的数组,包含通过回调函数测试的所有元素。
描述:
filter()方法创建一个新数组,其结果是该数组中的每个元素都是回调函数的结果。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.filter((element, index, array) => { return element > 3 }) console.log(result) // [4, 5]
1.4 reduce()
语法:
arr.reduce(callback(accumulator, currentValue, currentIndex, array), initialValue)参数:
callback:数组中每个元素需要调用的函数。accumulator:累加器,上一次调用回调函数时的返回值,或者是提供的初始值(initialValue)。currentValue:当前元素的值。currentIndex:当前元素的索引。array:被遍历的数组本身。initialValue:可选参数,传递给函数的值,该值被用作accumulator的第一个参数。
返回值:函数累计处理的结果。
描述:
reduce()方法接收一个函数作为参数,该函数接收四个参数:accumulator(累加器)、currentValue(当前元素的值)、currentIndex(当前元素的索引)、array(被遍历的数组本身)。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.reduce((accumulator, currentValue) => { return accumulator + currentValue }, 0) console.log(result) // 15reduceRight, reduceRight()方法与 reduce()方法类似,只是遍历的方向相反。
1.5 sort()
语法:
arr.sort([callback(a, b)])参数:
callback:可选参数,指定排序的比较函数。
返回值:排序后的数组。
描述:
sort()方法用于对数组进行排序,默认情况下,按照字符串的 Unicode 码点进行排序。示例:
jsconst arr = [3, 1, 2] const result = arr.sort() console.log(result) // [1, 2, 3]
1.6 some()
语法:
arr.some(callback(element, index, array), thisArg)参数:
callback:数组中每个元素需要调用的函数。thisArg:可选参数,执行回调函数时this绑定的对象。
返回值:布尔值,表示是否至少有一个元素通过了测试函数。
描述:
some()方法用于检测数组中的元素是否满足指定条件。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.some(item => item > 3) console.log(result) // true
1.7 every()
语法:
arr.every(callback(element, index, array), thisArg)参数:
callback:数组中每个元素需要调用的函数。thisArg:可选参数,执行回调函数时this绑定的对象。
返回值:布尔值,表示是否所有元素通过了测试函数。
描述:
every()方法用于检测数组中的元素是否满足指定条件。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.every(item => item > 0) console.log(result) // true
1.8 indexOf()
语法:
arr.indexOf(searchElement, fromIndex)参数:
searchElement:要查找的元素。fromIndex:可选参数,开始查找的位置。
返回值:找到的元素在数组中的索引,或者在未找到时返回-1。
描述:
indexOf()方法用于查找指定元素在数组中的索引。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.indexOf(3) console.log(result) // 2
1.9 lastIndexOf()
语法:
arr.lastIndexOf(searchElement, fromIndex)参数:
searchElement:要查找的元素。fromIndex:可选参数,开始查找的位置。
返回值:找到的元素在数组中的索引,或者在未找到时返回-1。
描述:
lastIndexOf()方法用于查找指定元素在数组中的最后一个索引。示例:
jsconst arr = [1, 2, 3, 4, 5, 3, 2, 1] const result = arr.lastIndexOf(3) console.log(result) // 5
1.10 find()
语法:
arr.find(callback(element, index, array), thisArg)参数:
callback:数组中每个元素需要调用的函数。thisArg:可选参数,执行回调函数时this绑定的对象。
返回值:数组中满足指定条件的第一个元素,或者在未找到时返回 undefined。
描述:
find()方法用于查找数组中满足指定条件的第一个元素。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.find(item => item > 3) console.log(result) // 4
1.11 findIndex()
语法:
arr.findIndex(callback(element, index, array), thisArg)参数:
callback:数组中每个元素需要调用的函数。thisArg:可选参数,执行回调函数时this绑定的对象。
返回值:数组中满足指定条件的第一个元素的索引,或者在未找到时返回-1。
描述:
findIndex()方法用于查找数组中满足指定条件的第一个元素的索引。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.findIndex(item => item > 3) console.log(result) // 3
1.12 fill()
语法:
arr.fill(value, start, end)参数:
value:要填充的值。start:可选参数,开始填充的位置。end:可选参数,结束填充的位置。
返回值:填充后的数组。
描述:
fill()方法用于将一个固定值填充到数组中。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.fill(0, 2, 4) console.log(result) // [1, 2, 0, 0, 5]
1.13 copyWithin()
语法:
arr.copyWithin(target, start, end)参数:
target:必需参数,从该位置开始替换数据。start:可选参数,从该位置开始读取数据,默认为 0。end:可选参数,到该位置前停止读取数据,默认为数组长度。
返回值:替换后的数组。
描述:
copyWithin()方法用于将数组内部指定位置的成员复制到其他位置(会覆盖原有成员),然后返回当前数组。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.copyWithin(0, 3) console.log(result) // [4, 5, 3, 4, 5]
1.14 includes()
语法:
arr.includes(valueToFind, fromIndex)参数:
valueToFind:必需参数,需要查找的元素值。fromIndex:可选参数,开始查找的位置。
返回值:布尔值,表示是否找到了该元素。
描述:
includes()方法用于判断当前数组是否包含某指定的值,如果是,则返回 true,否则返回 false。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.includes(3) console.log(result) // true
1.15 flat()
语法:
arr.flat([depth])参数:
depth:可选参数,表示要提取嵌套数组的结构深度,默认为 1。
返回值:一个新的扁平化的数组实例。
描述:
flat()方法用于将一个嵌套多层的数组(深度为depth,且默认depth为 1)转换为一个新的扁平化的数组。示例:
jsconst arr = [1, 2, [3, 4, [5, 6]]] const result = arr.flat() console.log(result) // [1, 2, 3, 4, [5, 6]]
1.16 flatMap()
语法:
arr.flatMap(callback(currentValue[, index[, array]])[, thisArg])参数:
callback:必需参数,数组中每个元素需要调用的函数。thisArg:可选参数,执行回调函数callback时this绑定的对象。
返回值:一个新的数组实例,该数组是通过执行回调函数
callback遍历原数组而生成的。描述:
flatMap()方法首先使用映射函数映射每个元素,然后将结果压缩成一个新数组。这个方法与map()和flat()连用,可以实现更灵活的扁平化操作。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.flatMap(x => [x * 2]) console.log(result) // [2, 4, 6, 8, 10]
1.17 array.from()
语法:
Array.from(arrayLike[, mapFn[, thisArg]])参数:
arrayLike:必需参数,一个类似数组的对象或可迭代对象。mapFn:可选参数,一个映射函数,可以用来对每个元素进行处理。thisArg:可选参数,执行回调函数mapFn时this绑定的对象。
返回值:一个新的数组实例,该数组是通过执行回调函数
mapFn遍历原数组而生成的。描述:
Array.from()方法从一个类似数组的对象或可迭代对象中创建一个新的数组实例。示例:
jsconst arrayLike = { length: 3, 0: 'a', 1: 'b', 2: 'c', } const result = Array.from(arrayLike) console.log(result) // ['a', 'b', 'c']
1.18 array.of()
语法:
Array.of(element0[, element1[, ...[, elementN]]])参数:
elementN:必需参数,要添加到数组末尾的元素。
返回值:一个新的数组实例,该数组是通过执行回调函数
mapFn遍历原数组而生成的。描述:
Array.of()方法创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型。示例:
jsconst result = Array.of(1, 2, 3) console.log(result) // [1, 2, 3]
1.19 array.at()
语法:
array.at(index)参数:
index:必需参数,要返回的数组元素索引。
返回值:指定索引处的元素。如果索引为负数,则将其作为 length + index 处理。如果索引超出范围,则返回 undefined。
描述:
Array.at()方法返回指定索引处的元素(或 undefined)。示例:
jsconst arr = [1, 2, 3, 4, 5] console.log(arr.at(2)) // 3 console.log(arr.at(-1)) // 5 console.log(arr.at(10)) // undefined
1.20 array.concat()
语法:
array.concat(value1[, value2[, ...[, valueN]]])参数:
valueN:可选参数,要合并到数组末尾的值或数组。
返回值:一个新数组,该数组是通过把所有
valueN合并到原数组array末尾生成的。描述:
Array.concat()方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。示例:
jsconst arr1 = [1, 2, 3] const arr2 = [4, 5, 6] const arr3 = [7, 8, 9] const result = arr1.concat(arr2, arr3) console.log(result) // [1, 2, 3, 4, 5, 6, 7, 8, 9]
1.21 pop()
语法:
array.pop()参数:无
返回值:被移除的元素。
描述:
Array.pop()方法从数组中删除最后一个元素,并返回该元素的值。此方法会更改数组的长度。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.pop() console.log(arr) // [1, 2, 3, 4] console.log(result) // 5
1.22 push()
语法:
array.push(value1[, value2[, ...[, valueN]]])参数:
valueN:可选参数,要添加到数组末尾的值。
返回值:该数组的新长度。
描述:
Array.push()方法将一个或多个元素添加到数组的末尾,并返回该数组的新的长度。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.push(6) console.log(arr) // [1, 2, 3, 4, 5, 6] console.log(result) // 6
1.23 shift()
语法:
array.shift()参数:无
返回值:被移除的元素。
描述:
Array.shift()方法从数组中删除第一个元素,并返回该元素的值。此方法会更改数组的长度。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.shift() console.log(arr) // [2, 3, 4, 5] console.log(result) // 1
1.24 unshift()
语法:
array.unshift(value1[, value2[, ...[, valueN]]])参数:
valueN:可选参数,要添加到数组开头的值。
返回值:该数组的新长度。
描述:
Array.unshift()方法将一个或多个元素添加到数组的开头,并返回该数组的新长度。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.unshift(0) console.log(arr) // [0, 1, 2, 3, 4, 5] console.log(result) // 6
1.25 slice()
语法:
array.slice([begin[, end]])参数:
begin:可选参数,要抽取的数组中的起始元素的索引。如果该参数为负数,则它会被当做array.length + begin看待。end:可选参数,规定从何处结束选取。该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从begin到数组结束的所有元素。如果该参数为负数,则它会被当做array.length + end看待。
返回值:一个新的数组,包含从
begin到end(不包括该元素)的 array 中的元素。描述:
Array.slice()方法返回一个新的数组,包含从begin到end(不包括该元素)的 array 中的元素。原始数组不会被改变。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.slice(1, 3) console.log(arr) // [1, 2, 3, 4, 5] console.log(result) // [2, 3]
1.26 splice()
语法:
array.splice(start[, deleteCount[, item1[, item2[, ...]]]])参数:
start:必需参数,规定添加/删除项目的位置,使用负数可以从数组结尾处规定位置。deleteCount:可选参数,要删除的数组元素的数量。如果deleteCount大于start之后的元素的总数,则从start后面的元素都将被删除(含第start个元素)。如果deleteCount被省略,或者它的值大于等于array.length - start(也就是说,如果它大于或者等于start之后的所有元素的数量),那么start之后数组的所有元素都会被删除。item1, item2, ...:可选参数,要添加到数组的新元素。
返回值:由被删除的元素组成的一个数组。如果只删除了一个元素,则返回只包含一个元素的数组。如果没有删除元素,则返回空数组。
描述:
Array.splice()方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。示例:
jsconst arr = [1, 2, 3, 4, 5] const result = arr.splice(1, 2, 6, 7) console.log(arr) // [1, 6, 7, 4, 5] console.log(result) // [2, 3]