1

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.

  • What have you tried so far? – Maheer Ali Apr 12 at 11:44
  • Hmm I was expecting for the same question. I have tried from this link but it did not work – Profer Apr 12 at 11:53
3

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>

  • No misunderstood my question. Please have a look at it again. The output does not match. – Profer Apr 12 at 12:22
  • Maybe you have mistake in your output, are you also filtering by deviceId? – Nenad Vracar Apr 12 at 12:23
  • Extremely sorry I have updated my question. Please have a look at my two points – Profer Apr 12 at 12:26
  • @Profer I've updated the answer, think this is it. – Nenad Vracar Apr 12 at 12:39
  • Perfect that is what I wanted. Thank a ton! – Profer Apr 12 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.