-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmode.js
39 lines (32 loc) · 805 Bytes
/
mode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'use strict';
module.exports = function mode(key) {
const values = [];
let highestCount = 1;
if (!this.items.length) {
return null;
}
this.items.forEach((item) => {
const tempValues = values.filter((value) => {
if (key !== undefined) {
return value.key === item[key];
}
return value.key === item;
});
if (!tempValues.length) {
if (key !== undefined) {
values.push({ key: item[key], count: 1 });
} else {
values.push({ key: item, count: 1 });
}
} else {
tempValues[0].count += 1;
const { count } = tempValues[0];
if (count > highestCount) {
highestCount = count;
}
}
});
return values
.filter(value => value.count === highestCount)
.map(value => value.key);
};