一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第4天,点击查看活动详情。
前言
数组的常见方法可以分成四大类:操作方法、排序方法、转换方法、迭代方法。不管是前端还是后端,对数据的基本操作都是增、删、改、查,接下来我们就从这四个大类总结一下数组的常用的方法。
操作方法
数组基本操作可以归纳为 增、删、改、查,需要留意的是哪些方法会对原数组产生影响
增
push
,unshift
,splice
,concat
,这四种为数组的增加的方法,但是前三种会修改原数据,最后一个则会返回新的数组,不会影响原数据。
push()
- push()方法接受一个任意数量的参数,并将它们添加到数组的
尾部
,返回数组的最新长度
let array = [
{id:1,name:'部门一'},
{id:2,name:'部门二'},
{id:3,name:'部门三'}
]
console.log("部门数组的长度",array.length)
array.push({id:4,name:'添加部门四'},{id:5,name:'添加部门五'})
console.log("添加之后的部门数组",array)
console.log("添加之后的部门数组的长度",array.length)
unshift()
- unshift()方法在数组
开头
添加任意多个值,然后返回新的数组的长度
let array = [
{id:1,name:'部门一'},
{id:2,name:'部门二'},
{id:3,name:'部门三'}
]
console.log("部门数组的长度",array.length)
array.unshift({id:4,name:'添加部门四'},{id:5,name:'添加部门五'})
console.log("添加之后的部门数组",array)
console.log("添加之后的部门数组的长度",array.length)
splice()
- splice() ,接受三个参数,分别是开始位置、0(要删除的元素的位置)、插入的元素,最后返回
空数组
let array = [
{id:1,name:'部门一'},
{id:2,name:'部门二'},
{id:3,name:'部门三'}
]
console.log("部门数组的长度",array.length)
// 从索引为1 的位置,添加内容
array.splice(1,0,{id:4,name:'添加部门四'},{id:5,name:'添加部门五'})
console.log("添加之后的部门数组",array)
console.log("添加之后的部门数组的长度",array.length)
eg:删除了index =2的部门二的数据之后,在添加上去
let array = [
{id:1,name:'部门一'},
{id:2,name:'部门二'},
{id:3,name:'部门三'}
]
console.log("部门数组的长度",array.length)
// 从索引为1 的位置,删除一项内容后,在添加新的内容
array.splice(1,1,{id:4,name:'添加部门四'},{id:5,name:'添加部门五'})
console.log("添加之后的部门数组",array)
console.log("添加之后的部门数组的长度",array.length)
concat()
- 首先会创建一个当前数组的副本,然后再把它的参数添加到副本末尾,最后返回这个新构建的数组,
不会
影响原始数组。
let array = [
{id:1,name:'部门一'},
{id:2,name:'部门二'},
{id:3,name:'部门三'}
]
console.log("部门数组的长度",array.length)
// 添加之后不会修改原数据
const arrConcat = array.concat({id:4,name:'添加部门四'})
console.log("原部门数组",array)
// 原始部门的数据长度根本没有改变
console.log("添加之后的原部门数组的长度",array.length) //3
console.log("新部门数组",arrConcat)
console.log("添加之后的新部门数组的长度",arrConcat.length)
删
pop
,shift
,splice
,slice
,前三种删除时会同时修改原数据,第四种则不会影响元数据
pop()
- pop()方法用于删除数组的
最后一项
,同时减少数组的length
值,返回被删除的项
let array = [
{id:1,name:'部门一'},
{id:2,name:'部门二'},
{id:3,name:'部门三'}
]
console.log("部门数组的长度",array.length)
// pop 删除最后一项数据
array.pop()
console.log("部门数组",array)
console.log("添加之后的部门数组的长度",array.length)
shift()
- shift()方法用于删除数组的
第一项
,同时减少数组的length
值,返回被删除的项
let array = [
{id:1,name:'部门一'},
{id:2,name:'部门二'},
{id:3,name:'部门三'}
]
console.log("部门数组的长度",array.length)
// pop 删除第一项数据
array.shift()
console.log("部门数组",array)
console.log("添加之后的部门数组的长度",array.length)
splice()
- 传入两个参数,分别是开始位置,删除元素的数量,返回包含删除元素的数组
let array = [
{id:1,name:'部门一'},
{id:2,name:'部门二'},
{id:3,name:'部门三'}
]
console.log("部门数组的长度",array.length)
// 删除第一项数据
array.splice(0,1)
console.log("部门数组",array)
console.log("删除之后的部门数组的长度",array.length)
slice() 用于创建一个包含原有数组中一个或者多个元素的新数组,不影响原数组
let array = [
{id:1,name:'部门一'},
{id:2,name:'部门二'},
{id:3,name:'部门三'},
{id:4,name:'部门4'},
{id:5,name:'部门5'},
{id:6,name:'部门6'}
]
console.log("部门数组的长度",array.length)
// 删除index 为1之前的数据
const arr1 = array.slice(1)
// 删除index 为1之前和index =4 之后的数据
const arr2 = array.slice(1,4)
console.log("部门数组",arr1)
console.log("删除之后的部门数组的长度",arr1.length)
console.log("部门数组",arr2)
console.log("删除之后的部门数组的长度",arr2.length)
改
修改原来的数组内容,可以使用splice
splice()
传入三个参数,分别是开始位置,要删除元素的数量,要插入的任意多个元素,返回删除元素的数组,对原数组产生影响
let array = [
{id:1,name:'部门一'},
{id:2,name:'部门二'},
{id:3,name:'部门三'},
]
console.log("部门数组的长度",array.length)
// 从index=1的位置开始,删除,再新增一条数据
array.splice(1,1,{id:4,name:'部门444'})
console.log("部门数组",array)
console.log("删除之后的部门数组的长度",array.length)
查
即查找元素,返回元素坐标或者元素值
indexOf()
- 返回要查找的元素在数组中的位置,如果没找到则返回
-1
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
numbers.indexOf(4) // 3
numbers.indexOf(8) // -1
find()
- 返回第一个匹配的元素
const people = [
{ name: "Matt", age: 27 },
{ name: "Nicholas", age: 29 }
];
people.find((element, index, array) => element.age < 28) // // {name: "Matt", age: 27}
includes()
- 返回要查找的元素在数组中的位置,找到返回
true
,否则false
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
numbers.includes(4) // true
排序方法
数组有两个方法可以用来对元素重新排序:reverse()
,sort()
reverse()
- 顾名思义,将数组元素方向排列
let values = [1, 2, 3, 4, 5];
values.reverse();
console.log(values); // 5,4,3,2,1
sort()
- sort()方法接受一个比较函数,用于判断哪个值应该排在前面
function compare(value1, value2) {
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
} }
let values = [0, 1, 5, 10, 15];
values.sort(compare);
console.log(values); // 0,1,5,10,15
转换方法
join()
- join() 方法接收一个参数,即字符串分隔符,返回包含所有项的字符串
let colors = ["red", "green", "blue"];
console.log(colors.join(",")); // red,green,blue
console.log(colors.join("||")); // red||green||blue
迭代方法
常用来迭代数组的方法(都不改变原数组)有如下:some()
,every()
,forEach()
,filter()
,map()
。
some()
- 对数组每一项都运行传入的函数,如果有一项函数返回 true ,则这个方法返回 true
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
let someResult = numbers.every((item, index, array) => item > 2);
console.log(someResult) // true
every()
- 对数组每一项都运行传入的函数,如果对每一项函数都返回 true ,则这个方法返回 true
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
let everyResult = numbers.every((item, index, array) => item > 2);
console.log(everyResult) // false
forEach()
- 对数组每一项都运行传入的函数,没有返回值
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
numbers.forEach((item, index, array) => {
// 执行某些操作
});
filter()
- 对数组每一项都运行传入的函数,函数返回
true
的项会组成数组之后返回
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
let filterResult = numbers.filter((item, index, array) => item > 2);
console.log(filterResult); // 3,4,5,4,3
map()
- 对数组每一项都运行传入的函数,返回由每次函数调用的结果构成的数组
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
let mapResult = numbers.map((item, index, array) => item * 2);
console.log(mapResult) // 2,4,6,8,10,8,6,4,2
最后
我正在成长,如果有什么问题欢迎大家留言,我们一起讨论。。。
如果对您有用,希望您留下评论/👍/收藏。
您的三连,是对我创作的最大的鼓励🙇♀️🙇♀️🙇♀️