js中Array数组常用的方法总结

1.push

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

pop()方法,删除数组最后一个元素,并将被删除的元素作为返回值返回,改变原数组

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

3.shift

shift()方法,删除数组第一个元素,将被删除元素作为返回值返回,改变原数组

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

4.unshift

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

作用:增删改
参数:ary.splice(index,howmany,item1,…,itemX)
第一个参数删除开始的索引,第二个参数,删除的个数,第三个参数删除位置添加新元素,改变原数组
返回值:删除的项
是否改变原数组:改变

删除的功能
ary.splice(n,m);
从数组的索引n开始,删除m项

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

6.slice

作用:截取数组(复制数组)
参数:array.slice(start, end)
返回值:返回一个新数组
是否改变原数组:不改变
注意:第一个参数开始索引(包含),第二个参数结束索引(不包含),索引可以取负值,-1倒数第一个,不改变原数组

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]

7.concat

将多个数组或值连接成新数组,并将新的数组返回,不改变原数组

var ary = [1,2,3,4,5];
var res = ary.concat(6,7);
var res2 = ary.concat(6,[7,8]);
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]

8.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

9.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]

10.indexOf

作用:查找指定元素的位置 参数:array.indexOf(item,start) item:查找的元素 start:字符串中开始检索的位置
返回值:返回第一次查到的索引,未找到返回-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

11. forEach

作用:循环遍历数组每一项
参数:函数 ary.forEach(function(item,index,ary){})
item:每一项 index:索引 ary:当前数组
返回值:无 是否改变原数组: 不改变

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  无返回值

12. map

作用:数组中的元素为原始数组元素调用函数处理后的值
参数:函数 ary.map(function(item,index,ary){}) item:每一项 index:索引 ary:当前数组
返回值:新数组
是否改变原数组:不改变

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

13. filter

作用:创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。(过滤)
参数:函数 ary.filter(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]
  • every
    作用:检测数组所有元素是否都符合指定条件
    参数:函数 ary.every(function(item,index,ary){}) item:每一项 index:索引 ary:当前数组
    返回值:布尔值
    是否改变原数组: 不改变
  • 如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
  • 如果所有元素都满足条件,则返回 true。

14. some

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

  • 如果有一个元素满足条件,则表达式返回 true , 剩余的元素不会再执行检测。
  • 如果没有满足条件的元素,则返回 false。
var ary = [1,2,3,4,5,6]
var res = ary.some(function(item){
    return item<3;
})
console.log(res)  // true;

15. find

find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。

var ages = [3, 10, 18, 20];
var item=ages.find(item=>item>10)
console.log(item)//18

16.findIndex

findIndex() 方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。

var ages = [3, 10, 18, 20];
const index=ages.find(item=>item>10)
console.log(index)//2