Skip to content

invertBy

js
_.invertBy(object, [iteratee=_.identity])

此方法与 _.invert 类似,只是反转的对象是从运行 objectiteratee 的每个元素的结果生成的。每个反转键对应的反转值是负责生成反转值的键数组。迭代器使用一个参数调用:(value)。

¥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).

新增于

¥Since

4.1.0

参数

¥Arguments

  1. object (对象):要修改的对象。

    ¥object (Object): The object to invert.

  2. [iteratee=_.identity] (函数):每个元素调用迭代器。

    ¥[iteratee=_.identity] (Function): The iteratee invoked per element.

返回

¥Returns

(对象):返回新的反转对象。

¥(Object): Returns the new inverted object.

示例

¥Example

js
var object = { 'a': 1, 'b': 2, 'c': 1 };

_.invertBy(object);
// => { '1': ['a', 'c'], '2': ['b'] }

_.invertBy(object, function(value) {
  return 'group' + value;
});
// => { 'group1': ['a', 'c'], 'group2': ['b'] }

Lodash v4.17 中文网 - 粤ICP备13048890号