> For the complete documentation index, see [llms.txt](https://lodash.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lodash.shujuwajue.com/collection/countby.md).

# \_.countBy（迭代处理数组或对象中的值）

**语法：**

```javascript
_.countBy(collection, [iteratee=_.identity])
```

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

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

**描述：**

创建一个组成对象，key（键）是经过`iteratee`（迭代函数） 执行处理`collection`中每个元素后返回的结果，每个key（键）对应的值是`iteratee`（迭代函数）返回该key（键）的次数（注：迭代次数）。 iteratee 调用一个参数：*(value)*。

**开始版本：**&#x30;.5.0

**参数：**

* `collection (Array|Object)`: 一个用来迭代的集合。
* `[iteratee=_.identity] (Array|Function|Object|string)`: 一个迭代函数，用来转换key（键）。

**返回值：**

* `(Object)`: 返回一个组成集合对象。

**例子：**

```javascript
_.countBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': 1, '6': 2 }

// The `_.property` iteratee shorthand.
_.countBy(['one', 'two', 'three'], 'length');
// => { '3': 2, '5': 1 }
```
