0

I'm looking for a best practice on how to group an array by the object's associated ID. The data looks like this:

const data = [
  {
    "id": 1,
    "association": {
      "id": 1,
      "name": "Association A"
    },
    "more_data": "...",
       ...
  },
  {
    "id": 2,
    "association": {
      "id": 2,
      "name": "Association B"
    },
    ...
  },
  ...
]

So the result would return:

  {
    1: [ { data_hash }, { data_hash }, ...], 
    2: [ { data_hash }, { data_hash }, ...],
    ...
  }

Where 1 and 2 are the association IDs above. Ruby/Rails has built in methods to do this easily:

Data.group_by(&:association_id).each do |association_id, data|
  puts association_id
  data.each do |data|
    puts data
  end
end

Is there something similar or equivalent in the JavaScript/ES6 world, or can anyone offer some insight on how to best handle this? A library, or even just a function that would do the trick?

The other added challenge here is that the association's ID sits in that nested hash.

EDIT: Not opposed to using a library at all. Looking for recommendations for something lightweight.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Greg Blass
  • 3,523
  • 29
  • 42
  • Nope. you'll have to pull in a library or do the looping/etc yourself. – Kevin B May 19 '17 at 18:15
  • 1
    lodash or underscore's groupBy is first thing that came to my mind – Travis White May 19 '17 at 18:18
  • 1
    So you're looking for lib recommendations? that's... riding the line of being off topic. but you could do this pretty easily with a simple map and/or reduce. – Kevin B May 19 '17 at 18:18
  • I'm more-so looking for an implementation and how to handle the nested id in that association hash. – Greg Blass May 19 '17 at 18:20
  • yeah, so use array reduce. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce also probably a few hundred [examples](http://stackoverflow.com/questions/39634912/group-javascript-array-of-objects-by-id) here on SO already. – Kevin B May 19 '17 at 18:20
  • Got it. I looked at that array reduce page but it wasn't immediately apparent how to use that to create a group_by function, but I'll look at it in more depth. – Greg Blass May 19 '17 at 18:22
  • please add a wanted result, more exactly ... – Nina Scholz May 19 '17 at 18:32
  • [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). There is no JSON in your example. – Felix Kling May 19 '17 at 23:59
  • Got it! Cool. I'm a JavaScript noob. Thanks for being nice about it. So I thought I was returning JSON. How is this not JSON? I'd love to learn. That's how you get better. – Greg Blass May 21 '17 at 03:40
  • Gotta say, so far the JS community in general on SO is just splendid. Real excited to get to deal with y'all. – Greg Blass May 21 '17 at 03:41
  • Is the whole 'Ruby would be like this' making everyone upset? I won't do that anymore if so. – Greg Blass May 21 '17 at 03:44

1 Answers1

0

It looks like lodash is the most lightweight library with a groupBy function:

https://lodash.com/docs/4.17.4#groupBy

The documentation there didn't make it very clear to me, but this SO question does:

using lodash .groupBy. how to add your own keys for grouped output?

And Vanilla version here using array reduce: https://stackoverflow.com/a/39635080/2202674 (thanks Kevin B)

Community
  • 1
  • 1
Greg Blass
  • 3,523
  • 29
  • 42