返回
JavaScript 功能数组实现
前端
2023-11-12 01:46:52
- 分割数组
分割数组是指将一个数组拆分成多个子数组。JavaScript 中有两种方法可以实现数组的分割:
- 使用
Array.prototype.slice()
方法:该方法可以从数组中截取指定范围的元素,并返回一个新的数组。例如:
const array = [1, 2, 3, 4, 5];
const subArray = array.slice(2, 4); // [3, 4]
- 使用
Array.from()
方法:该方法可以将一个类数组对象(如字符串、NodeList 等)转换为数组,也可以将一个数组分割成多个子数组。例如:
const array = [1, 2, 3, 4, 5];
const subArrays = Array.from(array, (value, index) => {
if (index % 2 === 0) {
return [value];
}
}); // [[1], [2], [3], [4], [5]]
2. 数组交集
数组交集是指两个或多个数组中都存在的元素。JavaScript 中有两种方法可以实现数组的交集:
- 使用
Array.prototype.filter()
方法:该方法可以从数组中筛选出满足指定条件的元素,并返回一个新的数组。例如:
const array1 = [1, 2, 3, 4, 5];
const array2 = [3, 4, 5, 6, 7];
const intersection = array1.filter(value => array2.includes(value)); // [3, 4, 5]
- 使用
Array.prototype.reduce()
方法:该方法可以将数组中的元素逐个累积,并返回一个最终值。例如:
const array1 = [1, 2, 3, 4, 5];
const array2 = [3, 4, 5, 6, 7];
const intersection = array1.reduce((result, value) => {
if (array2.includes(value)) {
result.push(value);
}
return result;
}, []); // [3, 4, 5]
3. 数组去重
数组去重是指去除数组中重复的元素,只保留唯一元素。JavaScript 中有两种方法可以实现数组的去重:
- 使用
Array.prototype.filter()
方法:该方法可以从数组中筛选出满足指定条件的元素,并返回一个新的数组。例如:
const array = [1, 2, 3, 4, 5, 1, 2, 3];
const uniqueArray = array.filter((value, index, self) => self.indexOf(value) === index); // [1, 2, 3, 4, 5]
- 使用
Array.prototype.reduce()
方法:该方法可以将数组中的元素逐个累积,并返回一个最终值。例如:
const array = [1, 2, 3, 4, 5, 1, 2, 3];
const uniqueArray = array.reduce((result, value) => {
if (!result.includes(value)) {
result.push(value);
}
return result;
}, []); // [1, 2, 3, 4, 5]
4. 数组排序
数组排序是指将数组中的元素按照一定的规则进行排序。JavaScript 中有两种方法可以实现数组的排序:
- 使用
Array.prototype.sort()
方法:该方法可以对数组中的元素进行排序。默认情况下,该方法会按照字符串的 Unicode 代码点进行排序。例如:
const array = [1, 2, 3, 4, 5];
array.sort(); // [1, 2, 3, 4, 5]
- 使用
Array.prototype.sort()
方法并传入一个比较函数:比较函数可以指定排序规则。例如:
const array = [1, 2, 3, 4, 5];
array.sort((a, b) => a - b); // [1, 2, 3, 4, 5]
5. 数组查找
数组查找是指在数组中找到满足指定条件的元素。JavaScript 中有两种方法可以实现数组的查找:
- 使用
Array.prototype.indexOf()
方法:该方法可以从数组中查找第一个与指定元素相等的元素,并返回其索引。如果未找到该元素,则返回 -1。例如:
const array = [1, 2, 3, 4, 5];
const index = array.indexOf(3); // 2
- 使用
Array.prototype.lastIndexOf()
方法:该方法可以从数组中查找最后一个与指定元素相等的元素,并返回其索引。如果未找到该元素,则返回 -1。例如:
const array = [1, 2, 3, 4, 5, 3];
const index = array.lastIndexOf(3); // 5
6. 数组循环
数组循环是指遍历数组中的每个元素,并执行某些操作。JavaScript 中有两种方法可以实现数组的循环:
- 使用
for
循环:for
循环可以逐个遍历数组中的元素。例如:
const array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
- 使用
forEach()
方法:forEach()
方法可以遍历数组中的每个元素,并执行指定的回调函数。例如:
const array = [1, 2, 3, 4, 5];
array.forEach((value, index, array) => {
console.log(value);
});
7. 数组合并
数组合并是指将两个或多个数组合并成一个新的数组。JavaScript 中有两种方法可以实现数组的合并:
- 使用
Array.prototype.concat()
方法:该方法可以将两个或多个数组合并成一个新的数组。例如:
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const mergedArray = array1.concat(array2); // [1, 2, 3, 4, 5, 6]
- 使用
Array.from()
方法:该方法可以将一个类数组对象(如字符串、NodeList 等)转换为数组,也可以将多个数组合并成一个新的数组。例如:
const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const mergedArray = Array.from(array1).concat(Array.from(array2)); // [1, 2, 3, 4, 5, 6]