3

The data looks like this:

const data = [
  {'a': '1', 'b': '2', 'c': '3'},
  {'a': '10', 'b': '20', 'c': '30'}
]

I want this:

const aArray = ['1','10']
     ,bArray = ['2', '20']
     ,cArray = ['3', '30']

I did this:

...
return {
  aArray = _.values(_.mapValues(data, 'a'))
  bArray = _.values(_.mapValues(data, 'b'))
  cArray = _.values(_.mapValues(data, 'c'))
}

It works but doesn't look clean. What's the cleanest way to group an object by multiple keys?

1
  • What exactly doesn't look clean? Looks very readable and short enough to me. Commented Aug 19, 2016 at 22:16

1 Answer 1

4

You can achieve this using plain JavaScript:

const data = [
  {'a': '1', 'b': '2', 'c': '3'},
  {'a': '10', 'b': '20', 'c': '30'}
]

const aArray = data.map(x => x.a)
     ,bArray = data.map(x => x.b)
     ,cArray = data.map(x => x.c)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.