Lodash - mapValues method



Syntax

_.mapValues(object, [iteratee=_.identity])

Creates an object with the same keys as object and values generated by running each own enumerable string keyed property of object thru iteratee. The iteratee is invoked with three arguments: (value, key, object).

Arguments

  • object (Object) − The object to iterate over.

  • [iteratee=_.identity] (Function) − The function invoked per iteration.

Output

  • (Object) − Returns the new mapped object.

Example

var _ = require('lodash');
var object = { 'a': 1, 'b': 2 };
var result = _.mapValues(object, function(value, key) {
   return key + value;
});

console.log(result);

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

{ a: 'a1', b: 'b2' }
lodash_object.htm
Advertisements