一、会改变原数组的方法
- puch:最后一位新增
// 返回的是 数组的长度
let arr = [1,3,4,5]
console.log(arr.push('22'));// 5
console.log(arr);// [1,3,4,5,'22']
- pop: 最后一位删除
let arr = [1,3,4,5]
console.log(arr.pop());// 5
console.log(arr);// [1,3,4]
- unshift:开头一位新增
// 返回的是 数组的长度
let arr = [1,3,4,5]
console.log(arr.unshift('哈哈哈'));// 5
console.log(arr);// ['哈哈哈',1,3,4,5]
- shift:开头第一位删除
let arr = [1,3,4,5]
console.log(arr.shift());// 1
console.log(arr);// [3,4,5]
- reverse:翻转
let arr = ['哈哈哈','我','吃'];
arr.reverse()
console.log(arr);// ['吃','我','哈哈哈']
- sort:排序
// 默认升序
let arr = [2,4,1,9,0];
arr.sort();
console.log(arr);// [0,1,2,4,9]
// 自定义降序
arr.sort(function(a,b){
return b-a
})
console.log(arr);// [9, 4, 2, 1, 0]
- splice(a,b,c):截取
// a:下标
// b:下标后的几位数
// c:要替换的内容
let arr = [3,"a",6,"b","hh","js","hello"];
arr.splice(3,2,"html"); // ["b","hh"]
console.log(arr); // [3,"a",6,"html","js","hello"];
console.log(arr.splice(2,2))// [6,'b']
console.log(arr);// [3,"a","hh","js","hello"]
console.log(arr.splice(2,0,'html'))// []
console.log(arr);// [3,"a",'html',"b","hh","js","hello"]
二、不会改变原数组的方法
-
concat
let arr = [3,"a",6,"b","hh","js","hello"]; let arr2 = [1,2] console.log(arr.concat(arr2));// [3,"a",6,"b","hh","js","hello",1,2] console.log(arr);// [3,"a",6,"b","hh","js","hello"];
-
join
// join方法的参数不传或者传入undefined会默认用逗号分隔。 let arr = [1,2,3,4]; console.log(arr.join(undefined))// 1,2,3,4 console.log(arr.join('、'));// 1、2、3、4
-
slice(a,b):截取a到b,不包含b
let arr = [3,"a",6,"b","hh","js","hello"]; console.log(arr.slice(2,4))// [6,"b"] console.log(arr);// [3,"a",6,"b","hh","js","hello"];
-
every
// 检测数组中是否有 所有数据 都符合条件 // 全部都是ture,一个不是false let arr = [3,"a",6,"b","hh","js","hello"]; let isFlag = arr.every(item=>{ if(item == 67){ return item; } }) console.log(isFlag);// false console.log(arr);// [3,"a",6,"b","hh","js","hello"]
-
some
// 检测数组中是否满足指定条件, // 如果有一个元素满足条件,则表达式返回true,剩余的元素不会再执行, // 如果没有满足条件的元素,则返回false(不会对空数组执行检测) let arr = [3,"a",6,"b","hh","js","hello"]; let isFlag = arr.some(item=>{ if(item == 6){ return item; } }) console.log(isFlag);// true console.log(arr);// [3,"a",6,"b","hh","js","hello"]
-
map
// let arr = [3,"a",6,"b","hh","js","hello"]; var m = arr.map(function(val,idx,self){ return val + "1"; }) console.log(arr);//[3,"a",6,"b","hh","js","hello"]; console.log(m);// ['31',"a1",'61',"b1","hh1","js1","hello1"]
-
filter
// 过滤,回调函数每次返回值为true, // 会将这个遍历到的值放到新数组中,在filter结束后,返回这个数组,如果为false则跳过 let arr = [3,"a",6,"b","hh","js","hello"]; var isFlag = arr.filter(function(val,idx,self){ return typeof val == "string"; }) console.log(isFlag ); // ['a', 'b', 'hh', 'js', 'hello']
-
reduce
// 求数组各项和 let arr = [1,2,3,4]; let sum = arr.reduce(function(pre,cur){ return pre+cur }) console.log(sum);// 10 // 取最大值 let max = arr.reduce(function(pre,cur){ return Math.max(pre,cur) }) console.log(sum);// 4 // 数组去重 let norepeat= arr.reduce(function(pre,cur){ pre.indexOf(cur) === -1 && pre.push(cur); return pre },[]) console.log(norepeat); // 将二维数组转换成一维数组 let arr = [[1,2],[3,4]]; let newArr = arr.reduce(function(pre,cur){ return pre.concat(cur) }) console.log(newArr );
-
reduceRight
```javascript
// 和reduce方法基本一样,但是入参方式从左往右
// 数组去重,倒序
let arr = [2,1,4,6,3]
let norepeat= arr.reduceRight(function(pre,cur){
pre.indexOf(cur) === -1 && pre.push(cur);
return pre
},[])
console.log(norepeat);// [3, 6, 4, 1, 2]
// 将二维数组转换成一维数组
let arr = [[1,2],[3,4]];
let newArr = arr.reduceRight(function(pre,cur){
return pre.concat(cur)
})
console.log(newArr );// [3,4,1,2]
```
- forEach
```javascript
var arr = ['a', 'b', 'hh', 'js', 'hello']
var f = arr.forEach(function(val,idx,self){
console.log(val); // 'a', 'b', 'hh', 'js', 'hello'
console.log(idx); //0 1 2 3 4 ......
})
console.log(f) //undefined 无返回值
console.log(arr); //原数组
```