返回

数组的功能与应用场景

前端

变异方法

变异方法直接改变数组本身。使用这些方法时,需要注意它们的返回值。有些方法返回修改后的数组,而另一些方法则返回一个新值,如删除的元素。

1. push() 方法

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

const fruits = ["apple", "orange", "banana"];
fruits.push("mango", "grapes");
console.log(fruits); // ["apple", "orange", "banana", "mango", "grapes"]

2. pop() 方法

pop() 方法从数组的末尾移除最后一个元素,并返回该元素。

const fruits = ["apple", "orange", "banana"];
const removedFruit = fruits.pop();
console.log(removedFruit); // "banana"
console.log(fruits); // ["apple", "orange"]

3. shift() 方法

shift() 方法从数组的开头移除第一个元素,并返回该元素。

const fruits = ["apple", "orange", "banana"];
const removedFruit = fruits.shift();
console.log(removedFruit); // "apple"
console.log(fruits); // ["orange", "banana"]

4. unshift() 方法

unshift() 方法向数组的开头添加一个或多个元素,并返回新的长度。

const fruits = ["apple", "orange", "banana"];
fruits.unshift("strawberry", "blueberry");
console.log(fruits); // ["strawberry", "blueberry", "apple", "orange", "banana"]

5. reverse() 方法

reverse() 方法颠倒数组中元素的顺序,并返回该数组。

const fruits = ["apple", "orange", "banana"];
fruits.reverse();
console.log(fruits); // ["banana", "orange", "apple"]

6. sort() 方法

sort() 方法对数组中的元素进行排序,并返回该数组。默认情况下,sort() 方法按字母顺序对字符串元素进行排序,按数字顺序对数字元素进行排序。

const numbers = [3, 1, 2, 5, 4];
numbers.sort();
console.log(numbers); // [1, 2, 3, 4, 5]

const fruits = ["apple", "banana", "orange"];
fruits.sort();
console.log(fruits); // ["apple", "banana", "orange"]

7. splice() 方法

splice() 方法用于添加、删除或替换数组中的元素。它接受三个参数:起始索引、要删除的元素数量和要添加的元素(可选)。

const fruits = ["apple", "orange", "banana"];

// 从索引 1 开始删除 1 个元素
fruits.splice(1, 1);
console.log(fruits); // ["apple", "banana"]

// 从索引 1 开始删除 1 个元素,并添加 "mango""grapes"
fruits.splice(1, 1, "mango", "grapes");
console.log(fruits); // ["apple", "mango", "grapes", "banana"]

非变异方法

非变异方法不会改变数组本身。这些方法返回一个新的数组,而不会影响原始数组。

1. concat() 方法

concat() 方法用于合并两个或多个数组,并返回一个新的数组。

const fruits1 = ["apple", "orange"];
const fruits2 = ["banana", "grapes"];
const allFruits = fruits1.concat(fruits2);
console.log(allFruits); // ["apple", "orange", "banana", "grapes"]

2. slice() 方法

slice() 方法用于从数组中提取一个子数组,并返回该子数组。它接受两个参数:起始索引和结束索引(可选)。

const fruits = ["apple", "orange", "banana", "grapes"];

// 从索引 1 开始提取子数组
const subFruits = fruits.slice(1);
console.log(subFruits); // ["orange", "banana", "grapes"]

// 从索引 1 开始,提取到索引 3(不包括索引 3)的子数组
const subFruits2 = fruits.slice(1, 3);
console.log(subFruits2); // ["orange", "banana"]

3. indexOf() 方法

indexOf() 方法用于查找数组中第一个匹配给定元素的元素的索引,如果找不到,则返回 -1。

const fruits = ["apple", "orange", "banana", "grapes"];

const index1 = fruits.indexOf("orange");
console.log(index1); // 1

const index2 = fruits.indexOf("strawberry");
console.log(index2); // -1

4. lastIndexOf() 方法

lastIndexOf() 方法用于查找数组中最后一个匹配给定元素的元素的索引,如果找不到,则返回 -1。

const fruits = ["apple", "orange", "banana", "orange", "grapes"];

const index1 = fruits.lastIndexOf("orange");
console.log(index1); // 3

const index2 = fruits.lastIndexOf("strawberry");
console.log(index2); // -1

5. includes() 方法

includes() 方法用于检查数组是否包含给定元素,如果包含,则返回 true,否则返回 false。

const fruits = ["apple", "orange", "banana", "grapes"];

const result1 = fruits.includes("orange");
console.log(result1); // true

const result2 = fruits.includes("strawberry");
console.log(result2); // false

6. find() 方法

find() 方法用于查找数组中第一个符合给定条件的元素,并返回该元素。如果找不到,则返回 undefined。

const fruits = ["apple", "orange", "banana", "grapes"];

const result = fruits.find((fruit) => fruit === "orange");
console.log(result); // "orange"

7. findIndex() 方法

findIndex() 方法用于查找数组中第一个符合给定条件的元素的索引,如果找不到,则返回 -1。

const fruits = ["apple", "orange", "banana", "grapes"];

const index = fruits.findIndex((fruit) => fruit === "orange");
console.log(index); // 1

数组的遍历方法

1. forEach() 方法

forEach() 方法用于遍历数组中的每个元素,并对每个元素执行给定的函数。

const fruits = ["apple", "orange", "banana", "grapes"];

fruits.forEach((fruit) => {
  console.log(fruit);
});

2. map() 方法

map() 方法用于遍历数组中的每个元素,并将其映射到一个新的数组。

const fruits = ["apple", "orange", "banana", "grapes"];

const newFruits = fruits.map((fruit) => {
  return fruit.toUpperCase();
});

console.log(newFruits); // ["APPLE", "ORANGE", "BANANA", "GRAPES"]

3. filter() 方法

filter() 方法用于遍历数组中的每个元素,并返回一个由符合给定条件的元素组成的数组。

const fruits = ["apple", "orange", "banana", "grapes"];

const citrusFruits = fruits.filter((fruit) => {
  return fruit === "orange" || fruit === "grapefruit";
});

console.log(citrusFruits); // ["orange"]

4. some() 方法

some() 方法用于遍历数组中的每个元素,并检查是否有至少一个元素满足给定的条件。如果至少有一个元素满足条件,则返回 true,否则返回 false。

const fruits = ["apple", "orange", "banana", "grapes"];

const result = fruits.some((fruit) => {
  return fruit === "strawberry";
});

console.log(result); // false

5. every() 方法

every() 方法用于遍历数组中的每个元素,并检查所有元素是否都满足给定的条件。如果所有元素都满足条件,则返回 true,否则返回 false。

const fruits = ["apple", "orange", "banana", "grapes"];

const result = fruits.every((fruit) => {
  return fruit.length > 5;
});

console.log(result); // false