Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

groupBy.js 675B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. var nestedValue = require('../helpers/nestedValue');
  3. module.exports = function groupBy(key) {
  4. var _this = this;
  5. var collection = {};
  6. this.items.forEach(function (item, index) {
  7. var resolvedKey = void 0;
  8. if (typeof key === 'function') {
  9. resolvedKey = key(item, index);
  10. } else if (nestedValue(item, key) || nestedValue(item, key) === 0) {
  11. resolvedKey = nestedValue(item, key);
  12. } else {
  13. resolvedKey = '';
  14. }
  15. if (collection[resolvedKey] === undefined) {
  16. collection[resolvedKey] = new _this.constructor([]);
  17. }
  18. collection[resolvedKey].push(item);
  19. });
  20. return new this.constructor(collection);
  21. };