Lodash invertBy method
语法
_.invertBy(object, [iteratee=_.identity])
This method is like _.invert except that the inverted object is generated from the results of running each element of object thru iteratee. The corresponding inverted value of each inverted key is an array of keys responsible for generating the inverted value. The iteratee is invoked with one argument: (value).
参数
object (Object) − The object to invert.
[iteratee=_.identity] (Function) − The iteratee invoked per element.
输出
(Object) − Returns the new inverted object.
实例
var _ = require('lodash');
var object = { 'a': 1, 'b': 2, 'c': 1 };
var result = _.invertBy(object);
console.log(result);
result = _.invertBy(object, function(value) {
return 'group' + value;
});
console.log(result);
Save the above program in
tester.js. Run the following command to execute this program.
Command
输出
{ '1': [ 'a', 'c' ], '2': [ 'b' ] }
{ group1: [ 'a', 'c' ], group2: [ 'b' ] }