在工作中很多地方都会用到数组,我们也经常会用数组来存数据,在处理数组数据的时候,JS为我们提供了很多内置的方法,下面就从数组方法的作用、是否需要参数、数组方法的返回值、是否会改变原有数组几个方面来写一下常用到的数组方法
1、pop
作用:删除数组的最后一项
参数:不需要参数
返回值:被删除的那一项
原数组:被改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.pop();
console.log(ary); ==> 输出结果:[1,29,391,182,39]
console.log(x); ==> 输出结果:29
2、push
作用:向数组的末尾添加一项或多项
参数:需要参数
返回值:新数组的length
原数组:被改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.push(29,19);
console.log(ary); ==> 输出结果:[1, 29, 391, 182, 39, 29, 29, 19]
console.log(x); ==> 输出结果:8
3、shift
作用:删除数组的第一项
参数:不需要参数
返回值:被删除的那一项
原数组:被改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.shift();
console.log(ary); ==> 输出结果:[29,391,182,39,29]
console.log(x); ==> 输出结果:1
4、unshift
作用:向数组的开头添加一项或多项
参数:需要参数
返回值:新数组的length
原数组:被改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.unshift(39,298,39);
console.log(ary); ==> 输出结果:[39,298,39,1,29,391,182,39,29]
console.log(x); ==> 输出结果:9
5、slice
作用:数组的截取
参数:需要参数
1) slice(m,n):从索引m开始截取到索引n,但不包含n,支持负数,如果是负数会先用数组的length和负数进行运算在进行截取,如ary.slice(2,-1);ary.lenght的是8那就是从索引2开始截取到索引7,但不包含索引7的内容
2) slice(m):从索引m开始截取到数组最后
3) slice():克隆一摸一样的数组
返回值:截取到的新数组
原数组:不被改变
代码演示:
- slice(m,n)
let ary = [1,29,391,182,39,29];
let x = ary.slice(2,7);
console.log(ary); ==> 输出结果:[1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果[391, 182, 39]
- slice(m)
let ary = [1,29,391,182,39,29];
let x = ary.slice(2);
console.log(ary); ==> 输出结果:[1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果[391, 182, 39, 29]
- slice()
let ary = [1,29,391,182,39,29];
let x = ary.slice();
console.log(ary); ==> 输出结果:[1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果 [1,29,391,182,39,29]
6、splice
作用:删除数组的某几项
参数:需要参数
1) splice(m,n,x):替换,从索引m开始删除n项,替换成x;如果n的值为0,则是在索引m前面新增
2) splice(m,n):从索引m开始删除n项
3) splice():从索引m开始删除到最后
返回值:被删除的几项组成的新数组
原数组:被改变
代码演示:
- splice(m,n,x)
let ary = [1,29,391,182,39,29];
let x = ary.splice(1,2,5);
console.log(ary); ==> 输出结果: [1, 5, 182, 39, 29]
console.log(x); ==> 输出结果: [29, 391]
- splice(m,n)
let ary = [1,29,391,182,39,29];
let x = ary.splice(1,2,5);
console.log(ary); ==> 输出结果: [1, 182, 39, 29]
console.log(x); ==> 输出结果: [29, 391]
- splice(m,n)
let ary = [1,29,391,182,39,29];
let x = ary.splice(1,2,5);
console.log(ary); ==> 输出结果: [1]
console.log(x); ==> 输出结果: [29, 391, 182, 39, 29]
7、sort
作用:排序
参数:可传可不传
1) sort():只对相同位数的进行比较排序
2) sort(function(a,b){return a-b;}):根据回调函数的返回结果,确定是从大到小还是从小到大
返回值:排序后的新数组
原数组:被改变
代码演示:
- sort()
let ary = [1,29,391,182,39,29];
let x = ary.sort();
console.log(ary); ==> 输出结果:[1, 182, 29, 29, 39, 391]
console.log(x); ==> 输出结果:[1, 182, 29, 29, 39, 391]
- sort(function(a,b){})
let ary = [1,29,391,182,39,29];
let x = ary.sort(function(a,b){
return a-b;
});
console.log(ary); ==> 输出结果: [1, 29, 29, 39, 182, 391]
console.log(x); ==> 输出结果: [1, 29, 29, 39, 182, 391]
注释:回调函数中返回的是a-b则是从小到大排列,返回的是b-a则是从大到小排列
8、reverse
作用:将数组成员颠倒显示
参数:不需要参数
返回值:颠倒顺序后的新数组
原数组:被改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.reverse();
console.log(ary); ==> 输出结果: [29, 39, 182, 391, 29, 1]
console.log(x); ==> 输出结果: [29, 39, 182, 391, 29, 1]
9、indexOf
作用:检测某个数组成员在数组中第一次出现时的索引
参数:需要参数
返回值:数组成员第一次在数组中出现时的索引,如检测的数组成员在数组中不存在返回-1
原数组:不改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.indexOf(10);
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果: -1
10、indexOf
作用:检测某个数组成员在数组中最后一次出现时的索引
参数:需要参数
返回值:数组成员最后一次在数组中出现时的索引,如检测的数组成员在数组中不存在返回-1
原数组:不改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.lastIndexOf(29);
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果: 5
11、concat
作用:数组拼接
参数:可传可不传
1) concat():将原数组克隆一份
2) concat(m,n):在数组的末尾添加m和n
返回值:拼接后的新数组
原数组:不改变
代码演示:
- concat()
let ary = [1,29,391,182,39,29];
let x = ary.concat();
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果: [1, 29, 391, 182, 39, 29]
- concat(1,2,3,4)
let ary = [1,29,391,182,39,29];
let x = ary.concat();
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果: [1, 29, 391, 182, 39, 29, 1, 2, 3, 4]
12、join
作用:把数组的每一项按照特定的字符连接成一个字符串
参数:需要参数
返回值:返回一个连接在一起的字符串
原数组:不改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.join('[]');
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果: 1[]29[]391[]182[]39[]29
13、map
作用:映射,可以映射成一个新数组
参数:需要参数
返回值:返回映射成的新数组
原数组:不改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.map(function(item,index){
return item + 1;
});
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果: [2, 30, 392, 183, 40, 30]
注释:回调函数中的item:指的数组的每一项,index指每一项的索引
14、forEach
作用:循环
参数:需要参数
返回值:forEach没有return,返回值是undefined
原数组:不改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.forEach(function(item,index){
return item + 1;
});
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果: undefined
注释:回调函数中的item:指的数组的每一项,index指每一项的索引
15、toString
作用:将数组转成字符串
参数:不需要参数
返回值:返回一个转换之后的字符串
原数组:不改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.forEach(function(item,index){
return item + 1;
});
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果: 1,29,391,182,39,29
16、find
作用:查找
参数:需要参数
返回值:返回查找到的第一个符合条件的数组成员
原数组:不改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.find(function(item,index){
return item > 30;
});
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果: 391
注释:回调函数中的item:指的数组的每一项,index指每一项的索引
find的机制:指查找第一个符合条件的,只要查找到了一个不再继续向下查找
17、filter
作用:过滤
参数:需要参数
返回值:返回一个过滤后的新数组
原数组:不改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.filter(function(item,index){
return item > 30;
});
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果: [391, 182, 39]
注释:回调函数中的item:指的数组的每一项,index指每一项的索引
filter的机制:查找所有符合条件的数组成员,组成一个新数组返回
18、every
作用:每一个
参数:需要参数
返回值:布尔值
原数组:不改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.every(function(item,index){
return item > 30;
});
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果:false
注释:回调函数中的item:指的数组的每一项,index指每一项的索引
every的机制:如果所以数组成员都符合条件才返回true,有一个不符合条件就返回false且不再向下循环
19、some
作用:一些
参数:需要参数
返回值:布尔值
原数组:不改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.some(function(item,index){
return item;
});
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果:true
注释:回调函数中的item:指的数组的每一项,index指每一项的索引
every的机制:只要有一个是true就返回true且不再向下循环,如果所有的都不符合条件返回false
20、includes
作用:包含
参数:需要参数
返回值:布尔值
原数组:不改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.includes(10);
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果:false
注释:回调函数中的item:指的数组的每一项,index指每一项的索引
every的机制:检测数组成员在数组中是否存在,存在返回true,不存在返回false
21、reduce
作用:收敛
参数:需要参数
返回值:回调函数的执行结果
原数组:不改变
代码演示:
let ary = [1,29,391,182,39,29];
let x = ary.reduce(function(prev,next){
return prev + next;
});
console.log(ary); ==> 输出结果: [1, 29, 391, 182, 39, 29]
console.log(x); ==> 输出结果:671
注释:回调函数中的prev:第一次循环指的是数组的第一个数组成员,之后都代表了上一次回调函数的执行结果;next指数组的每一项
回调函数可以传参数,如果回调函数传了参数,那prev第一次就指的是回调函数传的参数