数组常用方法总结

目录

push() pop() unshift() shift()

reserve() 

sort()

join() 

fill() 

concat() 

slice() 

splice() 

find()  findIndex()  includes()  indexOf()

forEach()

filter()

map()

every()

some() 

reduce()


push() pop() unshift() shift()

push()末尾,添加一个或更多元素,返回新数组的长度
pop()   末尾,删除数组元素,返回删除的元素
unshift() 开头,添加一个或更多元素,并返回新的长度
shift()开头,删除并返回数组的第一个元素

push() pop() unshift() shift()  改变原数组

示例:

var arr = [1,2,3,4];
console.log(arr.push(5,6,'a'));   // 7
console.log(arr);                 // [1, 2, 3, 4, 5, 6, 'a']
console.log(arr.pop());           // a
console.log(arr);                 // [1, 2, 3, 4, 5, 6]
console.log(arr.unshift(-1,0));   // 8
console.log(arr);                 //  [-1, 0, 1, 2, 3, 4, 5, 6]
console.log(arr.shift());         // -1
console.log(arr);                 // [0, 1, 2, 3, 4, 5, 6]

reserve() 

翻转数组  改变原数组

示例:

var arr = [1,2,3,4];
arr.reverse();           //  [4, 3, 2, 1]

sort()

排序

array.sort(sortfunction)

  • sortfunction  可选  函数
  • 默认 升序排序
  • 改变原数组

 示例:

var arr = [1,4,3,5,9,0];
arr.sort();   //  [0, 1, 3, 4, 5, 9]
arr.sort(function(a, b) {return b-a});   // [9, 5, 4, 3, 1, 0]

join() 

数组转字符串

array.join(separator)

  • separator  可选  分隔符  默认以' , ' 分隔
  • 不改变原数组

示例: 

var arr = ['hello', 'world'];
console.log(arr.join());      // hello,world
console.log(arr.join(' '));   // hello world

fill() 

将一个固定值替换数组的元素  

array.fill(value[, start, end]);
  • start  end  可选参数
  • 改变原数组

示例: 

var arr = [1,2,3,4];
console.log(arr.fill('a',1,3));     // [1, 'a', 'a', 4]
console.log(arr.fill('a'));         // ['a', 'a', 'a', 'a']

concat() 

连接两个或更多的数组,并返回结果  不改变原数组 

示例:

var arr = [1, 2];
console.log(arr.concat([11, 33], [22, [44]]));  // [1, 2, 11, 33, 22, [44]]

slice() 

截取

slice(startend)

  • 包括开始,不包括结尾
  • 参数为负数时,表示从原数组中的倒数第几个元素开始提取
  • 不改变原数组

示例: 

var arr = [0, 1, 2, 3];
console.log(arr.slice(1,3));     // [1, 2]
console.log(arr.slice());        // [0, 1, 2, 3]
console.log(arr.slice(1));       // [1, 2, 3]
console.log(arr.slice(-1));      // [3]
console.log(arr.slice(-3, -1));  // [1, 2]

splice() 

添加或删除元素

array.splice(index,howmany,item1,.....,itemX)

index必选。开始插入和(或)删除的数组元素的下标
howmany可选。删除多少元素。若未规定此参数,则删除从 index 开始到原数组结尾的所有元素
item1可选。要添加到数组的新元素

示例: 

var arr = ['aa', 'bb', 'cc', 'dd'];

// 删除元素:从下标为1的元素开始,删除两个元素
// console.log(arr.splice(1,2));    // ['bb', 'cc']
// console.log(arr);                // ['aa', 'dd']

// 删除元素:从下标为1的元素开始,删除到原数组结尾的所有元素
// console.log(arr.splice(1));      // ['bb', 'cc', 'dd'] 
// console.log(arr);                // ['aa']

// 添加:从下标为1的元素开始,删除0个元素,并添加元素 1, 2
// console.log(arr.splice(1, 0, 1, 2));      // []
// console.log(arr);                         // ['aa', 1, 2, 'bb', 'cc', 'dd']   

// 替换:删除下标为1的元素,并添加元素 1
// console.log(arr.splice(1, 1, 1));         // ['bb']
// console.log(arr);                         // ['aa', 1, 'cc', 'dd']

find()  findIndex()  includes()  indexOf()

find()返回符合传入测试(函数)条件的数组元素  || undefined
findIndex()返回符合传入测试(函数)条件的数组元素索引  || -1
includes() 判断一个数组是否包含一个指定的值    true  ||  false
indexOf() 搜索数组中的元素,并返回它所在的位置  ||  -1

find()  

array.find(function(currentValue, index, arr),thisValue)

currentValue必选。当前元素
index可选。当前元素的索引值
arr可选。当前元素所属的数组对象
thisValue可选。执行回调时用作this 的对象

示例: 

var arr = [4, 12, 16, 20];
var num = arr.find((val, index)=> { return val >= 12}));
// 当返回符合条件的元素,之后的值不会再调用执行函数
console.log(num);               // 12
console.log(arr);               // [4, 12, 16, 20]

findIndex()

array.findIndex(function(currentValue, index, arr), thisValue)
  • 参数与  find() 类似,返回满足条件的索引  或  -1
  • 并没有改变数组的原始值
  •  对于空数组,函数是不会执行的

示例:

var arr = ['aa', 'bb', 'cc', 'dd', 'aa'];
var index = arr.findIndex((val) => {return val == 'a'})
var index1 = arr.findIndex((val) => {return val == 'aa'})
console.log(index, index1);  // -1 0

includes()

arr.includes(searchElement, fromIndex)
searchElement

必须。需要查找的元素值

fromIndex

可选。从该索引处开始查找searchElement

如果为负值,则按升序从 array.length + fromIndex 的索引开始搜索,默认为 0

var arr = ['a', 'b', 'c'];
console.log(arr.includes('c', 2));      // true
console.log(arr.includes('c', 3));      // false
console.log(arr.includes('c', -1));     // true

indexOf()

array.indexOf(item,start)

item必须。查找的元素。
start可选。开始检索的位置。默认,从字符串的首字符开始检索。

示例:

var arr=["aa","bb","cc","aa","dd"];
console.log(arr.indexOf("aa"));         // 0
console.log(arr.indexOf("aa", 1));      // 3
console.log(arr.indexOf("aa", 4));      // -1

forEach()

数组每个元素都执行一次回调函数

示例:

arr.forEach(function(value, index, array) {
     // 参数一:数组元素
     // 参数二:数组元素的索引
     // 参数三:当前的数组
})
// 相当于数组遍历的 for循环 没有返回值
// 不能使用break跳出循环

filter()

检测数值元素,并返回符合条件所有元素的数组

示例:

var arr = [12, 66, 4, 88, 3, 7];
var newArr = arr.filter(function(value, index,array) {
   return value >= 20;
});
console.log(newArr);//[66,88] //返回值是一个新数组

map()

通过指定函数处理数组的每个元素,并返回处理后的数组

示例:

var arr = [0,1,2,3];
var flag = arr.map(function(value, index, array){
    return value*4;
});
console.log(flag);  // [0, 4, 8, 12]  ,所有元素都*4

every()

检测数值元素的每个元素是否都符合条件

示例:

var arr = [0,1,2,3];
var flag = arr.every(function(value, index, array){
    return value<=4;
});
console.log(flag); // 返回值是布尔值,若所有元素都满足条件,则返回true

some() 

检测数组元素中是否有元素符合指定条件

示例:

// some 查找数组中是否有满足条件的元素 
var arr = [10, 30, 4];
var flag = arr.some(function(value,index,array) {
   return value < 3;  // 返回true才终止迭代
});
console.log(flag); // 返回值是布尔值,只要查找到满足条件的一个元素就立马终止循环

reduce()

将数组元素计算为一个值(从左到右)

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
total必选。初始值, 或者计算结束后的返回值。
currentValue必选。当前元素
currentIndex可选。当前元素的索引
arr可选。当前元素所属的数组对象。
initialValue可选。传递给函数的初始值

 示例:

var arr = [1, 2, 3, 4];
console.log(arr.reduce((x, y) => x + y, 0));      // 10
console.log(arr.reduce((x, y) => x + y, 10));     // 20

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值