# \_.transform

**语法：**

```javascript
_.transform(object, [iteratee=_.identity], [accumulator])
```

**源代码链接：**[source](https://github.com/lodash/lodash/blob/4.17.10/lodash.js#L13787)

**npm包链接：**[npm package](https://www.npmjs.com/package/lodash.transform)

**描述：**

[`_.reduce`](https://lodash.com/docs/4.17.10#reduce)的替代方法;此方法将转换`object`对象为一个新的`accumulator`对象，结果来自`iteratee`处理自身可枚举的属性。 每次调用可能会改变`accumulator`对象。如果不提供`accumulator`，将使用与`[[Prototype]]`相同的新对象。`iteratee`调用4个参数：*(accumulator, value, key, object)*。如果返回`false`，`iteratee`会提前退出。

**开始版本：**&#x31;.3.0

**参数：**

* `object (Object)`: 要遍历的对象
* `[iteratee=_.identity] (Function)`: 每次迭代时调用的函数。
* `[accumulator] (*)`: 定制叠加的值。

**返回值：**

* `(*)`: 返回叠加后的值。

**例子：**

```javascript
_.transform([2, 3, 4], function(result, n) {
  result.push(n *= n);
  return n % 2 == 0;
}, []);
// => [4, 9]

_.transform({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {
  (result[value] || (result[value] = [])).push(key);
}, {});
// => { '1': ['a', 'c'], '2': ['b'] }
```
