Search code examples
javascriptarraysgroup-bylodash

Multilevel group by using lodash


I have below array of objects

const array = [
  {
    key: '11111',
    address: { city: 'NY', country: 'USA' },
    className: 'google.com',
    deviceId: 'aaaaa'
  },
  { 
    key: '11111',
    address: { city: 'NY', country: 'USA' },
    className: 'google.com',
    deviceId: 'aaaaa'
  },
  { 
    key: '33333',
    address: { city: 'NY', country: 'USA' },
    className: 'facebook.com',
    deviceId: 'aaaaa'
  },
  { 
    key: '11111',
    address: { city: 'NY', country: 'USA' },
    className: 'google.com',
    deviceId: 'ddddd'
  },
  {
    key: '22222',
    address: { city: 'Landon', country: 'UK' },
    className: 'stackoverflow.com',
    deviceId: 'ccccc'
  },
  {
    key: '22222',
    address: { city: 'Landon', country: 'UK' },
    className: 'stackoverflow.com',
    deviceId: 'ggggg'
  },
  {
    key: '22222',
    address: { city: 'Landon', country: 'UK' },
    className: 'stackoverflow.com',
    deviceId: 'fffff'
  }
]

I need the below output

[
  {
    "keys": {
      "address": { "city": "NY", "country": "USA" },
      "className": "google.com",
      "key": "11111"
    },
    "count": 2
  },
  {
    "keys": {
      "address": { "city": "NY", "country": "USA" },
      "className": "facebook.com",
      "key": "33333"
    },
    "count": 1
  },
  {
    "keys": {
      "address": { "city": "Landon", "country": "UK" },
      "className": "stackoverflow.com",
      "key": "22222"
    },
    "count": 3
  }
]

So what is really happening here.

1) I have to count the user as only one if the user is with the same key, address, className and deviceId.

and then

2) I want to group with the key, className and address and find the count.

How can I do that?

Thank you!!! Please ask if further information needed.


Solution

  • You could chain groupBy and map methods to first group by key, address, and className and then do one more groupBy to group and count elements with unique deviceId

    const array = [{"key":"11111","address":{"city":"NY","country":"USA"},"className":"google.com","deviceId":"aaaaa"},{"key":"11111","address":{"city":"NY","country":"USA"},"className":"google.com","deviceId":"aaaaa"},{"key":"33333","address":{"city":"NY","country":"USA"},"className":"facebook.com","deviceId":"aaaaa"},{"key":"11111","address":{"city":"NY","country":"USA"},"className":"google.com","deviceId":"ddddd"},{"key":"22222","address":{"city":"Landon","country":"UK"},"className":"stackoverflow.com","deviceId":"ccccc"},{"key":"22222","address":{"city":"Landon","country":"UK"},"className":"stackoverflow.com","deviceId":"ggggg"},{"key":"22222","address":{"city":"Landon","country":"UK"},"className":"stackoverflow.com","deviceId":"fffff"}]
    
    const result = _.chain(array)
      .groupBy(({ key, address: { city, country }, className }) => {
        return `${key}-${city}-${country}-${className}`
      }).map(e => {
        const [{ key, address, className }] = e;
        return {
          keys: { key, address, className },
          count: _.keys(_.groupBy(e, 'deviceId')).length
        }
      })
    
    console.log(result)
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script>