1👍
✅
Array reduce
to group "by" method.
var arr = [2, 2, 1.5, 1.5, 3, -2]
var result = arr.reduce(function(agg, item) {
var key = "other";
if (item < 1) key = "<1";
if (item > 1 && item < 2) key = "1<2";
if (item > 2) key = ">2";
agg[key] = (agg[key] || 0) + 1
return agg;
}, {})
console.log(result)
Source:stackexchange.com