- Underscore.JS 教程
- Underscore.JS - 主页
- Underscore.JS - 概述
- Underscore.JS - 环境设置
- Underscore.JS - 遍历集合
- Underscore.JS - 处理集合
- Underscore.JS - 遍历数组
- Underscore.JS - 处理数组
- Underscore.JS - 函数
- Underscore.JS - 映射对象
- Underscore.JS - 更新对象
- Underscore.JS - 比较对象
- Underscore.JS - 实用工具
- Underscore.JS - 链式操作
- Underscore.JS 实用资源
- Underscore.JS - 快速指南
- Underscore.JS - 实用资源
- Underscore.JS - 讨论
Underscore.JS - mapObject 方法
语法
_.mapObject(object, iteratee, [context])
mapObject 方法使用提供的 iteratee 函数转换对象中的每个值。请看以下示例 -
示例
var _ = require('underscore');
// Example 1
var result = _.mapObject({one: 1, two : 2, three: 3}, function(value, key){
return value * value;
});
console.log(result);
// Example 2
result = _.mapObject({ name: 'Sam', age: 30}, function(value, key){
return value + 10;
});
console.log(result);
将上述程序保存在 tester.js 中。运行以下命令执行此程序。
命令
\>node tester.js
输出
{ one: 1, two: 4, three: 9 }
{ name: 'Sam10', age: 40 }
underscorejs_mapping_objects.htm
广告