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.

41 lines
855 B

4 years ago
  1. 'use strict';
  2. module.exports = function mode(key) {
  3. var values = [];
  4. var highestCount = 1;
  5. if (!this.items.length) {
  6. return null;
  7. }
  8. this.items.forEach(function (item) {
  9. var tempValues = values.filter(function (value) {
  10. if (key !== undefined) {
  11. return value.key === item[key];
  12. }
  13. return value.key === item;
  14. });
  15. if (!tempValues.length) {
  16. if (key !== undefined) {
  17. values.push({ key: item[key], count: 1 });
  18. } else {
  19. values.push({ key: item, count: 1 });
  20. }
  21. } else {
  22. tempValues[0].count += 1;
  23. var count = tempValues[0].count;
  24. if (count > highestCount) {
  25. highestCount = count;
  26. }
  27. }
  28. });
  29. return values.filter(function (value) {
  30. return value.count === highestCount;
  31. }).map(function (value) {
  32. return value.key;
  33. });
  34. };