数组常用的方法

1. push:

  • 作用:向数组末尾追加一项
  • 参数:添加的具体项,可以是一项,也可以是多项
  • 返回值:新数组的长度
  • 是否改变原数组:改变

var ary=[‘a’,‘b’,‘c’];
var res= ary .push (‘d’,‘e’)
console.log(ary);//[‘a’,‘b’,‘c’,‘d’,‘e’];
console.log(res);//5

2. pop:

  • 作用:删除数组的最后一项
  • 参数:无
  • 返回值:删除的项
  • 是否改变原数组:改变

var ary = [‘1’,‘2’,‘3’];
var res = ary.pop();
console.log(ary); // [‘1’,‘2’];
console.log(res); // [‘3’];

3. shift:

  • 作用:删除数组的第一项
  • 参数:无
  • 返回值:删除的项
  • 是否改变原数组:改变

var ary = [‘a’,‘b’,‘c’];
var res = ary.shift();
console.log(ary); // [‘b’,‘c’];
console.log(res); // [‘a’];

4. unshift:

  • 作用:向数组的开头添加内容
  • 参数:添加的内容
  • 返回值:新数组的长度
  • 是否改变原数组:改变

var ary = [‘a’,‘b’,‘c’];
var res = ary.unshift(‘d’,‘e’);
console.log(ary); //[‘d’,‘e’,‘a’,‘b’,‘c’];
console.log(res); // 5

5. splice:

  • 作用:删除/新增/修改
  • 参数:splice(n,m,…x);完整的写法,第一个参数代表从哪个索引开始,删除m项,把删除的内容用x进行替换
  • 返回值:是一个数组,里面是删除的每一项
  • 是否改变原数组:是

增加功能
var ary = [1,2,3,4,5];
var res = ary.splice(1,0,6,7);
console.log(ary); // [1,6,7,2,3,4,5];
console.log(res); // [],删除0项,返回值是一个空数组

删除功能
var ary = [1,2,3,4,5];
var res = ary.splice(1,2);
console.log(ary); // [1,4,5];
console.log(res); // [2,3]

修改功能
var ary = [1,2,3,4,5];
var res = ary.splice(1,2,6,7);
console.log(ary); // [1,6,7,4,5];
console.log(res); // [2,3}

6.slice

  • 作用:从原有的数组中选中特定的内容
  • 参数:slice(n,m):选中从索引n(包含n)开始——索引m项(不包含m)
  • 返回值:返回值是一个数组,返回的每一项是复制的项
  • 是否改变原数组:不改变

var ary = [1,2,3,4,5];
var res = ary.slice(1,3);
var res2 = ary.slice(-3,-1)
console.log(ary); // [1,2,3,4,5];
console.log(res); // [2,3];
console.log(res2) // [3,4];

slice支持负参数,从最后一项开始算起,-1是最后一项,-2为倒数第二项
slice(n)从索引n开始复制到最后一项
slice()、slice(0):复制整个数组

7.concat

  • 作用:实现多个数组或者值的拼接
  • 参数:数组或者值
  • 返回值:返回值是拼接后的新数组
  • 是否改变原数组:不改变

var ary = [1,2,3,4,5];
var res = ary.concat(6,7);
var res2 = ary.concat(6,[7,8]);
var res3 = ary.concat(6,[7,[8,9]]);
var res4 = ary.concat();
console.log(ary); // [1,2,3,4,5];
console.log(res); // [1,2,3,4,5,6,7];
console.log(res2); //[1,2,3,4,5,6,7,8];
console.log(res3) // [1,2,3,4,5,6,7,[8,9]];
concat() 如果操作的参数是数组,那么添加的是数组中的元素,而不是数组。 如果是二维(或以上)数组,concat只能’拆开’一层数组
console.log(res4) // [1,2,3,4,5];
如果concat()没有参数或者参数是空数组也可以达到克隆数组的目的

8.toString

  • 作用:可以把一个逻辑值转换为字符串
  • 参数:无
  • 返回值:返回值是转换后的字符串
  • 是否改变原数组:不改变

var ary1=[1,2,3];
var res=ary1.toString();===>“1,2,3”

9.join

  • 作用:把数组通过指定的连接符,转换为字符串
  • 参数:连接符
  • 返回值:返回值是转换后的字符串
  • 是否改变原数组:不改变

var ary = [1,2,3,4,5];
var res = ary.join(’-’);
console.log(ary); // [1,2,3,4,5]
console.log(res); // ”1-2-3-4-5“

10.reverse 倒序

  • 作用:把数组倒过来
  • 参数:无
  • 返回值:返回值是排序后的新数组
  • 是否改变原数组:改变

var ary = [1,2,3,4,5];
var res = ary.reverse();
console.log(ary); //[[5,4,3,2,1]
console.log(res); // [5,4,3,2,1]

11.sort排序

  • 作用:把数组进行排序
  • 参数:无或者是一个函数
  • 返回值:排序后的新数组
  • 是否改变原数组:改变

var ary = [1,5,7,9,12,24,56,87,92];
var ary2 = [1,5,7,9,12,24,56,87,92];
var ary3 = [1,5,7,9,12,24,56,87,92];
var res = ary.sort();
var res2 = ary2.sort(function(a,b){
return a-b;
})
var res3 = ary3.sort(function(a,b){
return b-a;
})
// sort的参数函数总的形参a,b就是数组排序时候的相邻比较的两项
console.log(res); // [1, 12, 24, 5, 56, 7, 87, 9, 92]
console.log(res2); // [1,5,7,9,12,24,56,87,92]
console.log(res3); // [92,87,56,24,12,9,7,5,1]

不传参的时候,超出10以内的,只能按照第一位进行排序。
传参的时候, return a-b; // 升序, return b-a; //降序。

12.indexOf/lastlndexOf

  • 不兼容ie6-ie8:;

  • 作用:获取某项在数组中首次出现/最后出现的索引(也可以用来是否包含某项)

  • 参数

  • n:检测的项

  • m:如果是indexof的话,就是从索引m开始检索。如果是lastIndexOf的话,就是从索引m停止检索

  • 返回值:-1或者具体的索引值

  • 是否改变原数组:不改变

var ary = [1,2,3,4,5]
var res = ary.indexOf(3);
console.log(ary); // [1,2,3,4,5]
console.log(res); //2
var ary = [‘a’,‘b’,‘c’,‘d’,‘c’];
var res = ary.indexOf(‘c’,3);
console.log(res) //4

var ary = [‘a’,‘b’,‘c’,‘d’,‘c’];
var res = ary.lastIndexOf(‘c’,3);
var res2 = ary.lastIndexOf(‘c’,1);
console.log(res); //2
console.log(res2); //-1

13.includes

  • 作用:检测数组中是否包含某一项
  • 参数:具体项
  • 返回值:布尔值
  • 是否改变原数组:不改变

[1,2,3].includes(2);
true
[1,2,3].includes(100);
false

14.forEach

  • 作用:遍历数组中的每一项
  • 参数:函数
  • 返回值:undefined
  • 是否改变原数组:不改变

var ary = [‘a’,‘b’,‘c’]
var res = ary.forEach(function(item,index,ary){
console.log(item,index,ary);
// a 0 [“a”, “b”, “c”]
// b 1 [“a”, “b”, “c”]
// c 2 [“a”, “b”, “c”]
return item;
})

console.log(res) //undefined

15.map

  • 作用:把一个数组可以映射成一个新的数组
  • 参数:函数
  • 返回值:映射后的新数组
  • 是否改变原数组:不改变

var ary = [‘a’,‘b’,‘c’]
var res = ary.map(function(item,index,ary){
return item+1;
})
console.log(res) // [‘a1’,‘b1’,‘c1’]

16.filter

  • 作用:检测数组所有元素是否都符合指定条件
  • 参数:函数ary.every(function(item,index,ary){}) item:每一项index:索引ary:当前数组
  • 返回值:布尔值
  • 是否改变原数组:不改变

var ary = [1,2,3,4,5,6]
var res = ary.filter(function(item){
return item<3;
})
console.log(res) // [1,2]

17.some

  • 作用:检测数组中的元素是否满足指定条件
  • 参数:函数ary.some(function(item,index,ary){}) item:每一项 index:索引 ary:当前数组
  • 返回值:布尔值
  • 是否改变原数组:不改变

var ary = [1,2,3,4,5,6]
var res = ary.some(function(item){
return item<3;
})
console.log(res) // ture

1 如果有一个元素满足条件,则表达式返回 true , 剩余的元素不会再执行检测。
2 如果没有满足条件的元素,则返回 false。

18.every

  • 作用:检测数组所有元素是否都符合指定条件
    参数:函数ary.some(function(item,index,ary){}) item:每一项 index:索引 ary:当前数组
  • 返回值:布尔值
  • 是否改变原数组:不改变

var ary = [1,2,3,4,5,6]
var res = ary.every(function(item){
return item<3;
})
var res2 = ary.every(function(item){
return item<7;
})
console.log(res) //false
console.log(res2) // ture

1 如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
2 如果所有元素都满足条件,则返回 true。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值