Reputation: 1068
I have a input :
results = [
[ "Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS1", "small", "SW192EZ", "undefined" ],
[ "Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS2", "large", "SW192EZ", "undefined" ],
[ "Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS2", "large", "SW192EZ", "undefined" ],
[ "Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS2", "medium", "SW192EZ", "undefined" ],
]
I want the output to be something like
small
DryKISS1 : 1
medium
DryKISS2 : 1
large
DryKISS1 : 5
DryKISS2 : 2
DryKISS3 : 1
Basically grouping by size and then summing a company name in the same size bracket.
I have been playing with the below, but get lost as most of the examples are for hashes / objects
console.log _.chain( results ).groupBy( 4 ).map( ( value, key ) ->
[
key
_.reduce( value, ( ( result, currentObject ) ->
{
company: result[ 3 ]
}
))
]
).value()
Any help appreciated
Upvotes: 2
Views: 1860
Reputation: 386654
In plain Javascript, you could use an object with nested properties for the count.
var results = [["Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS1", "small", "SW192EZ", "undefined"], ["Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS2", "large", "SW192EZ", "undefined"], ["Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS2", "large", "SW192EZ", "undefined"], ["Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS2", "medium", "SW192EZ", "undefined"]],
grouped = {};
results.forEach(function (a) {
grouped[a[4]] = grouped[a[4]] || {};
grouped[a[4]][a[3]] = (grouped[a[4]][a[3]] || 0) + 1;
});
console.log(grouped);
With max count
var results = [["Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS1", "small", "SW192EZ", "undefined"], ["Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS2", "large", "SW192EZ", "undefined"], ["Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS2", "large", "SW192EZ", "undefined"], ["Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS2", "medium", "SW192EZ", "undefined"]],
grouped = {},
max = { count: 0, keys: [] };
results.forEach(function (a) {
grouped[a[4]] = grouped[a[4]] || {};
grouped[a[4]][a[3]] = (grouped[a[4]][a[3]] || 0) + 1;
if (grouped[a[4]][a[3]] > max.count) {
max = { count: grouped[a[4]][a[3]], keys: [{ size: a[4], group: a[3] }] };
return;
}
if (grouped[a[4]][a[3]] === max.count) {
max.keys.push = { size: a[4], group: a[3] };
}
});
console.log(max);
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 1
Reputation: 191996
Group by the 4th column, then use transform to count the items by the 3rd column:
var data = [
["Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS1", "small", "SW192EZ", "undefined"],
["Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS2", "large", "SW192EZ", "undefined"],
["Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS2", "large", "SW192EZ", "undefined"],
["Mon Nov 14 08:08:14 GMT+07:00 2016", "Ian Warner", "[email protected]", "DryKISS2", "medium", "SW192EZ", "undefined"],
];
var result = _(data)
.groupBy(4)
.transform(function(result, items, key) {
result[key] = _.countBy(items, 3);
})
.value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.0/lodash.min.js"></script>
Upvotes: 5