Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js map reduce

作者:情丶酸酸甜甜   发布日期:2026-07-02   浏览:32

// 使用 Map 和 Reduce 的示例代码

// 假设我们有一个数组,包含一些数字
const numbers = [1, 2, 3, 4, 5];

// 使用 map 方法将每个元素乘以 2
const doubledNumbers = numbers.map(number => number * 2);
console.log(doubledNumbers); // 输出: [2, 4, 6, 8, 10]

// 使用 reduce 方法计算所有元素的总和
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // 输出: 15

// 解释说明:
// - `map` 方法会遍历数组中的每一个元素,并对每个元素应用提供的函数,最终返回一个新数组。
// - `reduce` 方法会对数组中的每一个元素执行一个 reducer 函数(由你提供),并将结果汇总为单个输出值。第二个参数是初始值。

上一篇:js int转string

下一篇:js array map

大家都在看

js 数组打乱顺序

js 两个数组取交集

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js fill方法

js 数组连接

js json数组

js 数组复制

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站