// 使用 Lodash 的 `_.map` 方法对数组中的每个元素进行操作
const _ = require('lodash');
const numbers = [1, 2, 3, 4];
const squaredNumbers = _.map(numbers, n => n * n);
console.log(squaredNumbers); // 输出: [1, 4, 9, 16]
// 解释:
// 1. `require('lodash')` 引入了 Lodash 库。
// 2. `numbers` 是一个包含数字的数组。
// 3. `_.map` 是 Lodash 提供的一个方法,用于对数组中的每个元素执行指定的操作,并返回一个新的数组。
// 4. `n => n * n` 是一个箭头函数,表示将每个元素平方。
// 5. 最后使用 `console.log` 输出结果。
上一篇:nodejs 安装npm
下一篇:js 判断数组中是否有某个值
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站