js中数组常用的方法

1.push()向数组末尾添加元素,返回数组的长度

push()方法用于将一个或多个元素添加到数组的末尾,并返回新数组的长度。

例如,以下代码将数字5添加到名为myArray的数组的末尾

const myArray = [1, 2, 3, 4];
myArray.push(5);
console.log(myArray); // Output: [1, 2, 3, 4, 5]
2.pop()移除数组最后的元素

pop()方法用于从数组中删除最后一个元素,并返回该元素的值

例如,以下代码从名为myArray的数组中删除最后一个元素:

const myArray = [1, 2, 3, 4];
const lastElement = myArray.pop();
console.log(lastElement); // Output: 4
console.log(myArray); // Output: [1, 2, 3]
3.shift( )移除数组的第一个元素,返回移除元素的值

shift()方法用于从数组中删除第一个元素,并返回该元素的值。例如,以下代码从名为myArray的数组中删除第一个元素:

const myArray = [1, 2, 3, 4];
const firstElement = myArray.shift();
console.log(firstElement); // Output: 1
console.log(myArray); // Output: [2, 3, 4]
4.reverse() 翻转数组,返回翻转的结果

reverse()方法用于颠倒数组中元素的顺序

例如,以下代码将名为myArray的数组中的元素颠倒:

const myArray = [1, 2, 3, 4];
myArray.reverse();
console.log(myArray); // Output: [4, 3, 2, 1]
5.concat()连接两个数组,返回连接后的数组

concat()方法用于将两个或多个数组合并为一个数组

例如,以下代码将名为myArray1和myArray2的两个数组合并为一个数组:

const myArray1 = [1, 2, 3];
const myArray2 = [4, 5, 6];
const myArray3 = myArray1.concat(myArray2);
console.log(myArray3); // Output: [1, 2, 3, 4, 5, 6]
6. sort()升序
  • sort()方法用于对数组元素进行排序。默认情况下,sort()方法按升序对数组元素进行排序

例如,以下代码将名为myArray的数组中的元素按升序排序:

const myArray = [3, 1, 4, 2];
myArray.sort();
console.log(myArray); // Output: [1, 2, 3, 4]
  • sort()方法还可以接受一个比较函数作为参数,该函数定义了排序顺序

例如,以下代码将名为myArray的数组中的元素按降序排序:

const myArray = [3, 1, 4, 2];
myArray.sort((a, b) => b - a);
console.log(myArray); // Output: [4, 3, 2, 1]
7.slice ()截取数组,返回截取后的数组,不会影响原始数组的数据

slice()方法用于从数组中提取元素

例如,以下代码从名为myArray的数组中提取第二个和第三个元素:

const myArray = [1, 2, 3, 4];
const newArray = myArray.slice(1, 3);
console.log(newArray); // Output: [2, 3]
8.splice()数组剪接,可以对数组做很多添加,删除,更新的操作

splice()方法用于从数组中添加或删除元素。它接受三个参数:开始更改数组的索引要删除的元素数量要添加到数组中的元素,从开始索引开始

以下是使用splice()从数组中删除元素的示例:

const myArray = [1, 2, 3, 4];
myArray.splice(1, 1); // 删除索引为1的元素
console.log(myArray); // 输出:[1, 3, 4]

以下是使用splice()向数组中添加元素的示例:

const myArray = [1, 2, 3, 4];
myArray.splice(1, 0, 5); // 在索引1处添加元素5
console.log(myArray); // 输出:[1, 5, 2, 3, 4]
9. indexOf() 位置查找

用于查找数组中指定元素的第一个出现位置的索引。它接受一个参数,即要搜索的元素。如果在数组中找不到该元素,则indexOf()返回-1

以下是使用indexOf()在数组中查找元素索引的示例:

const myArray = [1, 2, 3, 4];
const index = myArray.indexOf(3);
console.log(index); // 输出:2
10.lastIndexOf()位置查找

用于查找数组中指定元素的最后一个出现位置的索引。它接受一个参数,即要搜索的元素。如果在数组中找不到该元素,则lastIndexOf()返回-1。

以下是使用lastIndexOf()在数组中查找元素索引的示例

const myArray = [1, 2, 3, 4, 2];
const index = myArray.lastIndexOf(2);
console.log(index); // 输出:4
11.forEach()迭代操作

forEach()方法用于对数组中的每个元素执行一次提供的函数。它接受一个参数,即要为每个元素执行的函数。该函数接受三个参数:当前元素当前元素的索引和正在遍历的数组

以下是使用forEach()迭代数组的示例:

const myArray = [1, 2, 3, 4];
myArray.forEach((element, index, array) => {
  console.log(`Element ${element} is at index ${index} in array ${array}`);
});
12.filter()数组过滤

filter()方法用于创建一个新数组,其中包含通过提供的函数实现的测试的所有元素。它接受一个参数,即测试每个元素的函数。该函数接受三个参数:当前元素当前元素的索引和正在遍历的数组

以下是使用filter()创建仅包含偶数的新数组的示例:

const myArray = [1, 2, 3, 4];
const evenNumbers = myArray.filter((element) => element % 2 === 0);
console.log(evenNumbers); // Output: [2, 4]
13.map() 数组映射,符合条件的数据重新赋值

map()方法用于创建一个新数组,其中包含通过提供的函数实现的测试的所有元素。它接受一个参数,即要为每个元素执行的函数。该函数接受三个参数:当前元素,当前元素的索引和正在遍历的数组

以下是使用map()创建一个新数组,其中包含原始数组中每个元素的平方的示例:

const myArray = [1, 2, 3, 4];
const squaredNumbers = myArray.map((element) => element ** 2);
console.log(squaredNumbers); // Output: [1, 4, 9, 16]
14.reduce()缩小方法,迭代所有的元素最终返回一个值

reduce()方法用于通过提供的函数将数组的所有元素累加到单个值中。它接受两个参数:要执行的函数和可选的初始值。该函数接受四个参数:累加器当前元素当前元素的索引和正在遍历的数组

以下是使用reduce()将数组中的所有元素相加的示例:

const myArray = [1, 2, 3, 4];
const sum = myArray.reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(sum); // Output: 10

如果您想使用初始值,请将其作为reduce()的第二个参数传递:

const myArray = [1, 2, 3, 4];
const sum = myArray.reduce((accumulator, currentValue) => accumulator + currentValue, 10);
console.log(sum); // Output: 20
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值