2

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

1
  • Could you explain more detail how you expect your supplied data, to become your expected output, as I'm no seeing it. For starters I can't see any DryKISS3 in your supplied data. Also is that you real email address, if so you do know scrappers could grab this and your going to get spammed.. :) Commented Nov 14, 2016 at 17:03

2 Answers 2

5

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Many thx I chose the JS version below but gave you an upvote also.
1

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; }

5 Comments

I want the output to be something like, doesn't look the same. :) It's most likely what he's after, but would be nice if OP's made the data source and expected results, reproducible.
Excellent thanks - I was trying to find a loDash way, but this is a simple vanilla method that should be used I feel.
Ahh one issue, how would I sort these so count is top?
object have no order. you yould get the keys in an array and sort the array. while youn have two levels, you need to sort the nesed level as well.
Wow - seems the sort is more complex then the iteration... many thanks, hope this helps others. Seems when I do a search for Array grouping I get so many replies that contain objects / collections or hashes depends on language

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.