js数组的方法
1.push和pop
push(): 把里面的内容添加到数组末尾,并返回修改后的长度。
pop():移除数组最后一项,返回移除的那个值,减少数组的length。
let ary = [2,4,6,8,10,12]; let ary1 = ary.push('新'); console.log(ary1) // 7 console.log(ary) // [2, 4, 6, 8, 10, 12, "新"]
let ary2 = ary.pop(); console.log(ary2) // 新 console.log(ary) // [2, 4, 6, 8, 10, 12]
2.unshift和shift
unshift(): 将参数添加到原数组开头,并返回数组的长度 。
shift():删除原数组第一项,并返回删除元素的值;如果数组为空则返回undefined 。
let ary = [2,4,6,8,10,12]; let ary1 = ary.unshift('新'); console.log(ary1) // 7 console.log(ary) // ["新", 2, 4, 6, 8, 10, 12]
let ary2 = ary.shift(); console.log(ary2) // 新 console.log(ary) // [2, 4, 6, 8, 10, 12]
3.join
join,就是把数组转换成字符串,然后给他规定个连接字符,默认的是逗号( ,),原数组不变
let ary = ['a','b','c']; console.log(ary.join()) //a,b,c console.log(ary.join('')) //abc console.log(ary.join(' ')) //a b c console.log(ary.join('-')) //a-b-c console.log(ary) //["a", "b", "c"]
4.sort
sort():将数组里的项从小到大排序,其比较的是字符串,没有按照数值的大小对数字进行排序,要实现这一点,就必须使用一个排序函数 改变原数组
let ary = ['a', 'd', 'x', 'b']; console.log(ary.sort()) //["a", "b", "d", "x"] console.log(ary) //["a", "b", "d", "x"] let ary1 = [21, 12, 3, 14] console.log(ary1.sort()) //[12, 14, 21, 3] ary1.sort((a, b) => { return a - b }) console.log(ary1) //[3, 12, 14, 21]
5.reverse
reverse():反转数组项的顺序。改变原数组
let ary = ['a','b','c']; console.log(ary.reverse()) //["c", "b", "a"] console.log(ary) //["c", "b", "a"]
6.concat
concat() :将参数添加到原数组中。这个方法会先创建当前数组一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组。在没有给 concat()方法传递参数的情况下,它只是复制当前数组并返回副本。原数组不变
let ary = [1,2,3]; let ary1 = ary.concat('a') console.log(ary1) //[1, 2, 3, "a"] console.log(ary) //[1, 2, 3] let ary2 = ary.concat(['c','d']) console.log(ary2) //[1, 2, 3, "c", "d"] console.log(ary) //[1, 2, 3]
7.slice
slice():返回从原数组中指定开始下标到结束下标之间的项组成的新数组。slice()方法可以接受一或两个参数,即要返回项的起始和结束位置。在只有一个参数的情况下, slice()方法返回从该参数指定位置开始到当前数组末尾的所有项。如果有两个参数,该方法返回起始和结束位置之间的项——但不包括结束位置的项。原数组不变
let ary = [2,4,6,10,8]; let ary1 = ary.slice(1) console.log(ary1) //[4, 6, 10, 8] console.log(ary) //[2, 4, 6, 10, 8] let ary2 = ary.slice(1,2) console.log(ary2) //[4]
8.splice 改变原数组
splice():删除、插入和替换
删除:两个参数:要删除的第一项的位置和要删除的项数。
插入:可以向指定位置插入任意数量的项,只需提供 3 个参数:起始位置、 0(要删除的项数)和要插入的项。第二个参数为0,返回空数组
替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定 3 个参数:起始位置、要删除的项数和要插入的任意数量的项。插入的项数不必与删除的项数相等。第二个参数为需要替换的项数,返回被替换的项数组
let ary = [2,4,6,8,10]; let ary1 = ary.splice(1,2) console.log(ary1) //[4, 6] console.log(ary) //[2, 8, 10] let ary2 = ary.splice(1,0,78,56); console.log(ary2) //[] console.log(ary) //[2, 78, 56, 8, 10] let ary3 = ary.splice(2,0,'a'); console.log(ary) //[2, 78, "a", 56, 8, 10] let ary4 = ary.splice(3,1,'c','d'); console.log(ary4) //[56] console.log(ary) //[2, 78, "a", "c", "d", 8, 10]
9.indexOf和lastIndeOf
indexOf(): 返回数组中元素在数组中的下标位置,从下标0开始查询,若数组中不存在改元素则返回-1
lastIndexOf(): 返回数组中元素在数组中的下标位置,从末尾开始查询
let ary = [5,7,7,8,9,1,1,4]; console.log(ary.indexOf(6)) //-1 console.log(ary.indexOf(5)) //0 console.log(ary.lastIndexOf(1)) //6
10.forEach
forEach():对数组进行遍历循环,对数组中的每一项运行给定函数。这个方法没有返回值。参数都是function类型,默认有传参,参数分别为:遍历的数组的每一项,该项对应的数组下标,数组本身。
let ary = [1, 2, 3, 4, 5]; ary.forEach((item, index) => { if(item == 2) { ary[index] = 7 } }) console.log(ary) // [1, 7, 3, 4, 5]
11.map
map():指“映射”,对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组。return返回新数组
let ary = [1, 2, 3, 4, 5]; let ary1 = ary.map((item,index)=>{ return item*item }) console.log(ary1) //[1, 4, 9, 16, 25] console.log(ary) //[1, 2, 3, 4, 5]
let ary = [1, 2, 3, 4, 5]; ary.map((item, index) => { if (item == 2) { ary[index] = 7 } }) console.log(ary) //[1, 7, 3, 4, 5]
12.filter
filter():“过滤”功能,数组中的每一项运行给定函数,返回满足过滤条件组成的数组。return返回新数组
let ary = [1, 2, 3, 4, 5]; let ary1 = ary.filter((item, index) => { return item > 2 }) console.log(ary1) //[3, 4, 5] console.log(ary) //[1, 2, 3, 4, 5]
let ary = [1, 2, 3, 4, 5]; ary.filter((item, index) => { if (item == 2) { ary[index] = 7 } }) console.log(ary) //[1, 7, 3, 4, 5]
13.every
every():判断数组中每一项都是否满足条件,只有所有项都满足条件,才会返回true。
let ary = [1, 2, 3, 4, 5]; let ary1 = ary.every(item => { return item < 6 }) let ary2 = ary.every(item => { return item < 5 }) console.log(ary1) //true console.log(ary2) //false
14.some
some():判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true。
let ary = [1, 2, 3, 4, 5]; let ary1 = ary.some(item => { return item < 2 }) let ary2 = ary.some(item => { return item < 1 }) console.log(ary1) //true console.log(ary2) //false
15.reduce
reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。
arr.reduce(function(prev,cur,index,arr){
...
}, init);
arr 表示原数组;
prev 表示上一次调用回调时的返回值,或者初始值 init;
cur 表示当前正在处理的数组元素;
index 表示当前正在处理的数组元素的索引,若提供 init 值,则索引为0,否则索引为1;
init 表示初始值。
1).求数组之和
let ary = [3, 9, 4, 3, 6, 11, 0, 9]; let ary1 = ary.reduce((prev, cur) => { return prev + cur }, 0) console.log(ary1) //45
由于传入了初始值0,所以开始时prev的值为0,cur的值为数组第一项3,相加之后返回值为3作为下一轮回调的prev值,然后再继续与下一个数组项相加,以此类推,直至完成所有数组项的和并返回。(传入了init则开始时prev为init,若无则为数组第一项)
2).求数组项最大值
let ary = [3, 9, 4, 3, 6, 11, 0, 9]; let ary2 = ary.reduce((prev, cur, index, ary) => { return prev > cur ? prev : cur }, 0) console.log(ary2) //11 let ary3 = ary.reduce((prev, cur) => { return prev > cur ? prev : cur }) console.log(ary3) //11 let ary4 = ary.reduce((prev, cur) => { return Math.max(prev, cur) }) console.log(ary4) //11
由于未传入初始值,所以开始时prev的值为数组第一项3,cur的值为数组第二项9,取两值最大值后继续进入下一轮回调。
3).扁平一个二维数组
let ary = [[1, 2, 8], [3, 4, 9], [5, 6, 10]]; let ary1 = ary.reduce((prev, cur) => { return prev.concat(cur) }, []); console.log(ary1) //[1, 2, 8, 3, 4, 9, 5, 6, 10]
16.find
find()方法,用于找出第一个符合条件的数组成员。它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该成员。如果没有符合条件的成员,则返回undefined。
[1, 2, 5, -1, 9].find((n) => n < 0) //找出数组中第一个小于 0 的成员 // -1
17.findIndex
findIndex()方法的用法与find()方法非常类似,返回第一个符合条件的数组成员的位置,如果所有成员都不符合条件,则返回-1。
[1, 2, 5, -1, 9].findIndex((n) => n < 0) //返回符合条件的值的位置(索引) // 3
浙公网安备 33010602011771号