多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## partition + [link](./partition "Link to this entry.") + [source](https://github.com/lodash/lodash/blob/4.5.0正式版/lodash.src.js#L7979 "View in source.") + [npm](https://www.npmjs.com/package/lodash.partition "See the npm package.") ``` _.partition(collection, [predicate=_.identity]) ``` 创建一个拆分为两部分的数组。 第一部分是 `predicate` 检查为真值的,第二部分是 `predicate` 检查为假值的。 predicate 会传入3个参数:(value, index|key, collection)。 ### 参数 1. collection (Array|Object) 需要遍历的集合 2. [predicate=_.identity] (Function|Object|string) 这个函数会处理每一个元素 ### 返回值 (Array) 返回分组元素的数组 ### 示例 ``` var users = [ { 'user': 'barney', 'age': 36, 'active': false }, { 'user': 'fred', 'age': 40, 'active': true }, { 'user': 'pebbles', 'age': 1, 'active': false } ]; _.partition(users, function(o) { return o.active; }); // => 结果: [['fred'], ['barney', 'pebbles']] // 使用了 `_.matches` 的回调结果 _.partition(users, { 'age': 1, 'active': false }); // => 结果: [['pebbles'], ['barney', 'fred']] // 使用了 `_.matchesProperty` 的回调结果 _.partition(users, ['active', false]); // => 结果: [['barney', 'pebbles'], ['fred']] // 使用了 `_.property` 的回调结果 _.partition(users, 'active'); // => 结果: [['fred'], ['barney', 'pebbles']] ```