1. forEach
forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。没有返回值,本质上等同于 for 循环,对每一项执行 function 函数。不会改变原数组。
// currentValue:必需,当前元素 index:可选,当前元素的索引值 arr:可选,当前元素所属的数组对象。
array.forEach(function(currentValue, index, arr))
复制代码
let array = ['a', 'b', 'c']
let func = (currentValue, index, arr) => {
currentValue += 's'
console.log('currentValue:' + currentValue + ' index:' + index + ' arr:' + arr)
}
array.forEach(func)
console.log(array)
// 控制台输出:
// currentValue:as index:0 arr:a,b,c
// currentValue:bs index:1 arr:a,b,c
// currentValue:cs index:2 arr:a,b,c
// [ 'a', 'b', 'c' ]
复制代码
2. map
通过指定函数处理数组的每个元素,并返回处理后的数组。
map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。方法按照原始数组元素顺序依次处理元素。不会改变原数组。
// currentValue:必须,当前元素的值 index:可选,当前元素的索引值 arr:可选,当前元素属于的数组对象
array.map(function(currentValue,index,arr))
复制代码
let array = [1, 2, 3, 4, 5]
let result = array.map((item) => {
return item += 5
})
console.log(array)
console.log(result)
// [ 1, 2, 3, 4, 5 ]
// [ 6, 7, 8, 9, 10 ]
复制代码
3. concat
JavaScript中的 concat()
方法用来连接两个或更多的数组,并返回结果。
// array1, array2, ..., arrayN 必需,该参数可以是具体的值,也可以是数组对象,可以是任意多个
array1.concat(array2,array3,...,arrayN)
复制代码
const array1 = ['a', 'b', 'c']
const array2 = ['d', 'e', 'f']
const array3 = array1.concat(array2)
console.log(array3)
const array4 = array1.concat('123')
console.log(array4)
// [ 'a', 'b', 'c', 'd', 'e', 'f' ]
// [ 'a', 'b', 'c', '123' ]
复制代码
4. push
Javascript数组中的 push()
方法用来向数组的末尾添加一个或更多元素,并返回新的长度。
let fruits = ["Banana", "Orange", "Apple", "Mango"]
let length = fruits.push("Kiwi")
console.log(fruits)
console.log(length)
// [ 'Banana', 'Orange', 'Apple', 'Mango', 'Kiwi' ]
// 5
复制代码
5. unshift
unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。
let fruits = ["Banana", "Orange", "Apple", "Mango"]
let length = fruits.unshift("Lemon", "Pineapple")
console.log(fruits)
console.log(length)
// [ 'Lemon', 'Pineapple', 'Banana', 'Orange', 'Apple', 'Mango' ]
// 6
复制代码
6. pop
*pop()
真题解析、进阶学习笔记、最新讲解视频、实战项目源码、学习路线大纲
详情关注公中号【编程进阶路】
方法用于删除数组的最后一个元素并返回删除的元素。*
let sites = ['Google', 'Runoob', 'Taobao', 'Zhihu', 'Baidu']
let result = sites.pop()
console.log(sites)
console.log(result)
// [ 'Google', 'Runoob', 'Taobao', 'Zhihu' ]
// Baidu
复制代码
7. shift
shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值
let fruits = ["Banana", "Orange", "Apple", "Mango"];
let result = fruits.shift()
console.log(fruits)
console.log(result)
// [ 'Orange', 'Apple', 'Mango' ]
// Banana
复制代码
8. splice
splice() 方法用于添加或删除数组中的元素,并返回删除的元素数组
// 参数 Values: index: 必需,规定从何处添加/删除元素
// howmany: 可选,规定应该删除多少元素 必须是数字,但可以是 "0"
// item1, ..., itemX 可选,要添加到数组的新元素
array.splice(index,howmany,item1,.....,itemX)
复制代码
let fruits = ["Banana", "Orange", "Apple", "Mango"]
let result = fruits.splice(1, 2, "Lemon", "Kiwi")
console.log(fruits)
console.log(result)
// [ 'Banana', 'Lemon', 'Kiwi', 'Mango' ]
// [ 'Orange', 'Apple' ]
复制代码
9. slice
slice() 方法可从已有的数组中返回选定的元素。也可提取字符串的某个部分,并以新的字符串返回被提取的部分。不会改变原数组。
// start: 可选,规定从何处开始选取 若为负值,表示从原数组中的倒数第几个元素开始提取
// end: 可选,规定从何处结束选取 如果没有指定该参数,那么切分的数组包含从start到数组结束的所有元素
array.slice(start, end)
复制代码
let fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]
let result1 = fruits.slice(1, 3)
let result2 = fruits.slice(2)
console.log(fruits)
console.log(result1)
console.log(result2)
// [ 'Banana', 'Orange', 'Lemon', 'Apple', 'Mango' ]
// [ 'Orange', 'Lemon' ]
// [ 'Lemon', 'Apple', 'Mango' ]
复制代码
10. join
join() 方法可将所有数组元素结合为一个字符串。它的行为类似 toString(),但是您还可以规定分隔符
// separator: 可选,指定要使用的分隔符 如果省略该参数,则使用逗号作为分隔符
array.join(separator)
复制代码
let fruits = ["Banana", "Orange", "Apple", "Mango"];
let energy1 = fruits.join();
let energy2 = fruits.join('-');
console.log(energy1)
console.log(energy2)
// Banana,Orange,Apple,Mango
// Banana-Orange-Apple-Mango
复制代码
11. every
every() 方法用于检测数组所有元素是否都符合指定条件(通过函数提供)。
array.every(function(currentValue,index,arr))
复制代码
let ages = [32, 33, 16, 40]
let nums = [32, 33, 19, 40]
function checkAdult(age) {
return age >= 18
}
function checkNums(num) {
return num >= 18
}
// 16不满足大于18,故结果false
let result1 = ages.every(checkAdult)
// 每一项都满足条件,故结果true
let result2 = nums.every(checkNums)
console.log(result1)
console.log(result2)
// false
// true
复制代码
12. filter
filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。不会改变原数组。
array.filter(function(currentValue,index,arr), thisValue)
复制代码
let ages = [32, 33, 16, 40];
function checkAdult(age) {
return age >= 18;
}
let result = ages.filter(checkAdult)
console.log(result)
// [ 32, 33, 40 ]
复制代码
13. indexOf
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。没有找到会返回-1
// searchvalue: 必需。规定需检索的字符串值。
// start: 可选的整数参数。规定在字符串中开始检索的位置。值:0~array.length-1
string.indexOf(searchvalue,start)
复制代码
let str = "Hello world, welcome to the universe.";
// 输出w所在的下标索引13(空格也算),没有找到会返回-1
let n = str.indexOf("welcome");
console.log(n)
console.log(str[n])
// 13
// w
复制代码
14. reduce
reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
复制代码
let numbers = [2, 3, 5, 6]
function getSum(total, num) {
return total + num
}
let result = numbers.reduce(getSum, 0)
console.log(result)
// 16
复制代码
15. reverse
reverse() 方法用于颠倒数组中元素的顺序。会改变原数组,并返回改变顺序的数组。
最后
由于篇幅限制,pdf文档的详解资料太全面,细节内容实在太多啦,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!
l, num) {
return total + num
}
let result = numbers.reduce(getSum, 0)
console.log(result)
// 16
复制代码
#### 15. reverse
*reverse() 方法用于颠倒数组中元素的顺序。会改变原数组,并返回改变顺序的数组。*
### 最后
由于篇幅限制,pdf文档的详解资料太全面,细节内容实在太多啦,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!
[外链图片转存中...(img-2XDeVk1c-1720775109478)]
[外链图片转存中...(img-26P4xanK-1720775109481)]