JavaScript基本操作数组方法

1、访问元素:

通过索引访问元素:使用方括号和索引来获取数组中指定位置的元素。

const array = [1, 2, 3];
console.log(array[0]); // 输出: 1

获取数组长度:使用 length 属性获取数组的长度。

const array = [1, 2, 3];
console.log(array.length); // 输出: 3

2、增加元素:

添加元素到数组末尾:使用 push() 方法将元素添加到数组的末尾(会改变原数组)。

const array = [1, 2, 3];
array.push(4);
console.log(array); // 输出: [1, 2, 3, 4]

添加元素到数组开头:使用 unshift() 方法将元素添加到数组的开头(会改变原数组)。

const array = [1, 2, 3];
array.unshift(0);
console.log(array); // 输出: [0, 1, 2, 3]

在指定位置插入元素:使用 splice() 方法在指定索引处插入元素(会改变原数组)。

const array = [1, 2, 3];
array.splice(1, 0, 1.5);
console.log(array); // 输出: [1, 1.5, 2, 3]

3、删除元素:

删除数组末尾的元素:使用 pop() 方法删除数组末尾的元素,并返回删除的元素(会改变原数组)。

const array = [1, 2, 3];
const removedElement = array.pop();
console.log(array); // 输出: [1, 2]
console.log(removedElement); // 输出: 3

删除数组开头的元素:使用 shift() 方法删除数组开头的元素,并返回删除的元素(会改变原数组)。

const array = [1, 2, 3];
const removedElement = array.shift();
console.log(array); // 输出: [2, 3]
console.log(removedElement); // 输出: 1

删除指定位置的元素:使用 splice() 方法删除指定索引处的元素(会改变原数组)。

const array = [1, 2, 3];
array.splice(1, 1);
console.log(array); // 输出: [1, 3]

4、修改元素:

修改指定位置的元素:通过索引和赋值操作修改指定位置的元素(会改变原数组)。

const array = [1, 2, 3];
array[1] = 5;
console.log(array); // 输出: [1, 5, 3]

5、数组遍历:

使用 for 循环遍历数组(不会改变原数组):

const array = [1, 2, 3];
for (let i = 0; i < array.length; i++) {
  console.log(array[i]);
}

使用 forEach() 方法遍历数组(不会改变原数组):

const array = [1, 2, 3];
array.forEach((item, index, array)=> {
  console.log(item); // 输出当前元素
  console.log(index); // 输出当前索引
  console.log(array); // 输出原始数组
});

6、查找元素:

使用 find() 方法查找第一个满足条件的元素(不会改变原数组):

const array = [1, 2, 3, 4, 5];
const foundElement = array.find((item) => {
  return item > 3; // 查找大于3的元素
});
console.log(foundElement); // 输出: 4

使用 findIndex() 方法查找第一个满足条件的元素的索引(不会改变原数组):

const array = [1, 2, 3, 4, 5];
const foundIndex = array.findIndex((item) => {
  return item > 3; // 查找大于3的元素的索引
});
console.log(foundIndex); // 输出: 3

7、数组过滤:

使用 filter() 方法过滤数组找到所有符合条件的元素(不会改变原数组):

const array = [1, 2, 3, 4, 5];
const filteredArray = array.filter((item) => {
  return item % 2 === 0; // 过滤偶数
});
console.log(filteredArray); // 输出: [2, 4]

8、数组映射:

使用 map() 方法映射数组(不会改变原数组):

const array = [1, 2, 3];
const mappedArray = array.map((item) => {
  return item * 2; // 每个元素乘以2
});
console.log(mappedArray); // 输出: [2, 4, 6]

9、数组排序:

使用 sort() 方法对数组进行排序(会改变原数组):

const ascendArray = [3, 1, 2];
const descendArray = [3, 1, 2];
// 升序排序
ascendArray.sort((a, b) => a - b);
console.log(ascendArray); // 输出: [1, 2, 3]
// 降序排序
descendArray.sort((a, b) => b - a);
console.log(descendArray); // 输出: [3, 2, 1]

10、数组连接:

使用 concat() 方法将多个数组连接成一个新数组(不会改变原数组):

const array1 = [1, 2];
const array2 = [3, 4];
const newArray = array1.concat(array2);
console.log(newArray); // 输出: [1, 2, 3, 4]

11、数组切片:

使用 slice() 方法从原数组中获取指定范围的元素(不会改变原数组):

const array = [1, 2, 3, 4, 5];
const slicedArray = array.slice(1, 3);
console.log(slicedArray); // 输出: [2, 3]

12、查找元素索引:

使用 indexOf() 方法查找元素在数组中的索引(不会改变原数组):

const array = [1, 2, 3, 2];
console.log(array.indexOf(2)); // 输出: 1
console.log(array.lastIndexOf(2)); // 输出: 3

13、数组反转:

使用 reverse() 方法反转数组元素的顺序(会改变原数组):

const array = [1, 2, 3];
array.reverse();
console.log(array); // 输出: [3, 2, 1]

14、数组检查:

使用 some() 方法检查数组中是否存在满足条件的元素:

const array = [1, 2, 3, 4, 5];
const hasEvenNumber = array.some((item) => {
  return item % 2 === 0; // 检查是否存在偶数
});
console.log(hasEvenNumber); // 输出: true

使用 every() 方法检查数组中的所有元素是否都满足条件:

const array = [1, 2, 3, 4, 5];
const allGreaterThanZero = array.every((item) => {
  return item > 0; // 检查是否所有元素都大于0
});
console.log(allGreaterThanZero); // 输出: true

15、数组累计运算:

使用 reduce() 方法对数组进行累计操作:

const array = [1, 2, 3, 4, 5];
const sum = array.reduce((accumulator, currentValue) => {
  return accumulator + currentValue;
}, 0);
console.log(sum); // 输出: 15

猜你喜欢

转载自blog.csdn.net/weixin_44523517/article/details/131809253