Array常用的函数

在JavaScript中,Array 对象提供了许多实用的方法来操作数组。以下是常用的几个函数及其简要说明:

1. Array.prototype.forEach

forEach 用于遍历数组中的每个元素,并执行一个提供的函数。

语法
array.forEach(callback(currentValue[, index[, array]])[, thisArg])
示例
const numbers = [1, 2, 3, 4, 5];
numbers.forEach((value, index) => {
  console.log(`Value at index ${index}: ${value}`);
});

2. Array.prototype.map

map 创建一个新数组,其结果是调用一个提供的函数产生的数组每一个元素的新值。

语法
array.map(callback(element[, index[, array]])[, thisArg])
示例
const numbers = [1, 2, 3, 4, 5];
const squaredNumbers = numbers.map(number => number * number);
console.log(squaredNumbers); // 输出: [1, 4, 9, 16, 25]

3. Array.prototype.filter

filter 创建一个新数组,其包含通过所提供函数实现的测试的所有元素。

语法
array.filter(callback(element[, index[, array]])[, thisArg])
示例
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(number => number % 2 === 0);
console.log(evenNumbers); // 输出: [2, 4]

4. Array.prototype.reduce

reduce 对数组中的每个元素执行一个由您提供的 reducer 函数(升序执行),将其结果汇总为单个返回值。

语法
array.reduce(callback(accumulator, currentValue[, currentIndex[, array]])[, initialValue])
示例
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // 输出: 15

5. Array.prototype.some

some 测试数组中是否有至少一个元素通过了由提供的函数定义的测试。

语法
array.some(callback(element[, index[, array]])[, thisArg])
示例
const numbers = [1, 2, 3, 4, 5];
const hasEven = numbers.some(number => number % 2 === 0);
console.log(hasEven); // 输出: true

6. Array.prototype.every

every 测试数组中的所有元素是否都通过了由提供的函数定义的测试。

语法
array.every(callback(element[, index[, array]])[, thisArg])
示例
const numbers = [2, 4, 6, 8, 10];
const allEven = numbers.every(number => number % 2 === 0);
console.log(allEven); // 输出: true

7. Array.prototype.find

find 返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined

语法
array.find(callback(element[, index[, array]])[, thisArg])
示例
const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
];
const user = users.find(u => u.id === 2);
console.log(user); // 输出: { id: 2, name: 'Bob' }

8. Array.prototype.findIndex

findIndex 返回数组中满足提供的测试函数的第一个元素的索引。否则返回 -1

语法
array.findIndex(callback(element[, index[, array]])[, thisArg])
示例
const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
];
const index = users.findIndex(u => u.name === 'Bob');
console.log(index); // 输出: 1

9. Array.prototype.concat

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

语法
array.concat([item1[, item2[, ...[, itemN]]]])
示例
const numbers1 = [1, 2, 3];
const numbers2 = [4, 5, 6];
const combined = numbers1.concat(numbers2);
console.log(combined); // 输出: [1, 2, 3, 4, 5, 6]

10. Array.from

Array.from 用于从类数组对象或可迭代对象创建一个新的数组实例。

语法
Array.from(arrayLike[, mapFn[, thisArg]])
示例
const arrayLike = { '0': 'a', '1': 'b', '2': 'c', length: 3 };
const array = Array.from(arrayLike);
console.log(array); // 输出: ['a', 'b', 'c']
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值