js数组⽅法总结⽬录
1、push() 尾插
(1)只插⼊⼀个元素
const arr = [1, 2, 3, 4, 5]
const arr1 = arr.push(6)
console.log(arr);//[1,2,3,4,5,6]
consloe.log(arr1);//6 返回值是数组的长度
(2)插⼊多个元素
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.push(7, 8, 9)
console.log(arr);//[1,2,3,4,5,6,7,8,9]
console.log(arr1);//9 返回值是数组的长度
(3)插⼊数组
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.push([7, 8, 9])
console.log(arr);//[1,2,3,4,5,6,[7,8,9]]
console.log(arr1);//7
(可以使⽤解构赋值将数组展开插⼊ ...[7,8,9])
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.push(...[7, 8, 9])
console.log(arr);//[1,2,3,4,5,6,7,8,9]
console.log(arr1);//9
(4)添加对象
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.push({ id: 8, name: 9, age: 10 })
console.log(arr);//[1,2,3,4,5,6,{ id: 8, name: 9, age: 10 }]
console.log(arr1);//7
2、pop() 尾删
只能删除⼀个
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.pop()
console.log(arr);//[1,2,3,4,5]
console.log(arr1);//6 返回的是被删除的元素
3、unshift() 头插
与尾插是⼀样的,可以插⼊多个
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.unshift(2)
console.log(arr);//[2,1,2,3,4,5,6]
console.log(arr1);//7 返回的是数组的长度
4、shift() 头删
与尾删⼀样,只能删除⼀个
filter过滤对象数组const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.shift()
console.log(arr);//[2,3,4,5,6]
console.log(arr1);//1 返回的是删除的数据
5、splice() 删除多个数据且可以添加数据(1)只有⼀个参数时
参数是⼏,就从下标为⼏的数据开始删除
const arr = [1, 2, 3, 4, 5, 6, 7]
const arr1 = arr.splice(1)
console.log(arr);//[1]
console.log(arr1);//[2,3,4,5,6] 返回删除的数组
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.splice(3)
console.log(arr);//[1,2,3]
console.log(arr1);//[4,5,6]
如果参数为负数
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.splice(-3)
console.log(arr);//[1,2,3]
console.log(arr1);//[4,5,6]
其实很好理解,如图所⽰
(2)有两个参数时
表⽰从规定下标开始删除n个数,第⼆个参数为0时也不会截取数组
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.splice(1,3)
console.log(arr);//[1,5,6]
console.log(arr1);//[2,3,4]
负数也是这样的,从下标为-3的开始删除3个数
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.splice(-3, 3)
console.log(arr);//[1,2,3]
console.log(arr1);//[4,5,6]
两个数同为负数,就不会成功截取
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.splice(-3, -1)
console.log(arr);//[1,2,3,4,5,6]
console.log(arr1);//[]
(3)有三个参数时
[1,3,6] 表⽰数据从下标1开始删除3个数据,将6添加进去,就像头插尾插⼀样,可以添加多个
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.splice(1,3,6)
console.log(arr);//[1,6,5,6]
console.log(arr1);//[2,3,4]
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.splice(1,3,6,7,8) //从第三个参数开始,后⾯都是添加的数据
console.log(arr);//[1,6,7,8,5,6]
console.log(arr1);//[2,3,4]
有负数的话其实也好理解
const arr = [1, 2, 3, 4, 5, 6]
const arr1 = arr.splice(-3, 2, 8)
console.log(arr);//[1, 2, 3, 8, 6]
console.log(arr1);//[4,5]
6、concat() 连接多个数组
const arr = [1, 2, 3]
const arr1 = [4, 5, 6]
const arr2 = [7, 8, 9]
const arr3 = at(arr1,arr2)
console.log(arr3);//[1,2,3,4,5,6,7,8,9]
ES6中可以使⽤解构赋值来完成这⼀操作,效果是⼀样的
const arr = [1, 2, 3]
const arr1 = [4, 5, 6]
const arr2 = [7, 8, 9]
const arr3 = [...arr,...arr1,...arr2]
console.log(arr3);//[1,2,3,4,5,6,7,8,9]
7、str.split() 将字符串转化为数组
const str = "hello"
const str1 = str.split("")
console.log(str1);//["h", "e", "l", "l", "o"]
其中split()中的""是⽤来分割字符串的,意思就是⽤什么去分割字符串,前提是字符串中必须存在split()中写⼊的标识
const str = "h,e,l,l o"
const str1 = str.split(",")
console.log(str1);//["h", "e", "l", "l o"]
8、sort() 排序
(1)默认排序
按照字符串的形式排序,也就是只认定最左边的数字
let arr = [1, 5, 4, 3, 12, 32, 6, 7, 98, 111]
console.log(arr.sort()); //[1, 111, 12, 3, 32, 4, 5, 6, 7, 98]
//简单的例⼦
console.log("111">"12") //false
(2)从⼩到⼤排序
const arr = [1, 5, 4, 3, 12, 32, 6, 7, 98, 111]
const arr2 = arr.sort((a, b) => a - b )
console.log(arr2); //[1, 3, 4, 5, 6, 7, 12, 32, 98, 111]
(3)从⼤到⼩排序
const arr = [1, 5, 4, 3, 12, 32, 6, 7, 98, 111]
const arr2 = arr.sort((a, b) => b - a )
console.log(arr2); //[111, 98, 32, 12, 7, 6, 5, 4, 3, 1]
9、reverse() 反转数组
const arr = [1, 2, 3, 4, 5]
const arr2 = verse()
console.log(arr2);//[5,4,3,2,1]
10、slice() 截取数组
(1)只有⼀个参数时
表⽰从那个数组下标开始截取,slice(2),从下标为2的开始截取
const arr = [1, 2, 3, 4, 5]
const arr2 = arr.slice(2)
console.log(arr2);//[3,4,5] 返回的是截取的数组
如果是负数
const arr = [1, 2, 3, 4, 5]
const arr2 = arr.slice(-2)
console.log(arr2);//[4,5]
(2)有两个参数时
[2,4) 表⽰从下标2开始,到下标4之前结束,取不到5,就是下标2到下标3的数据,这⾥就是开始位置到结束位置
const arr = [1, 2, 3, 4, 5]
const arr2 = arr.slice(2, 4)
console.log(arr2);//[3,4]
负数的话
const arr = [1, 2, 3, 4, 5]
const arr2 = arr.slice(-3, -1)
console.log(arr2);//[3,4]
总结splice和slice的区别
(1)splice是删除数组(返回的是删除的数组),slice是截取数组(返回值是截取的数组)
(2)splice(n,m,k)中n是开始位置,m是删除的长度,k是插⼊的数据,slice(n,m)中n是开始位置,m是结束位置
(3)splice可以传3个参数,slice只能传2个
11、forEach(callback)
对数组进⾏遍历,⾥⾯的回调函数有三个参数,value是当前的索引值,index是当前索引,array是原数组,三个参数可以任选写⼊,不必三个都写
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论