0

I have array of objects as follows:

[
    {
        company: "CompanyName",
        id: "1",

        userProfile: {
            id: "2",            
            telephone: "",
            user: {
                email: "some_email",
                firstName: "Firstname",
                lastName: "Lastname",
                groups: [
                    {id: "2", name: "Manager"},
                    {id: "10", name: "Remarketing Manager"}
                ]
            }
        }
    },
    {
        company: "CompanyName",
        id: "2",

        userProfile: {
            id: "3",
            telephone: "",
            user: {
                email: "some_email",
                firstName: "Firstname",
                lastName: "Lastname",
                groups: [
                    {id: "1", name: "Seller"}
                ]
            }
        }
    },
    {
        company: "CompanyName",
        id: "3",

        userProfile: {
            id: "4",
            telephone: "",
            user: {
                email: "some_email",
                firstName: "Firstname",
                lastName: "Lastname",
                groups: [
                    {id: "2", name: "Manager"}
                ]
            }
        }
    }
]

I want to count by group name.

Thus the result that I want is:

{
   "Manager": 2,
   "Seller": 1,
   "Remarketing Manager": 1,
}

I tried with lodash countBy as follows:

countBy(users, 'userProfile.user.groups.name');

But it doesn't work.

Here is the fiddle.

2 Answers 2

3

Flatten the arrays of groups with _.flatMap(), and then count by name:

const data = [{"company":"CompanyName","id":"1","userProfile":{"id":"2","telephone":"","user":{"email":"some_email","firstName":"Firstname","lastName":"Lastname","groups":[{"id":"2","name":"Manager"},{"id":"10","name":"Remarketing Manager"}]}}},{"company":"CompanyName","id":"2","userProfile":{"id":"3","telephone":"","user":{"email":"some_email","firstName":"Firstname","lastName":"Lastname","groups":[{"id":"1","name":"Seller"}]}}},{"company":"CompanyName","id":"3","userProfile":{"id":"4","telephone":"","user":{"email":"some_email","firstName":"Firstname","lastName":"Lastname","groups":[{"id":"2","name":"Manager"}]}}}]

const result = _.countBy(_.flatMap(data, 'userProfile.user.groups'), 'name')

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

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

Comments

1

You can use reduce

let data = [{company: "CompanyName",id: "1",userProfile: {id: "2",telephone: "",user: {email: "some_email",firstName: "Firstname",lastName: "Lastname",groups: [{id: "2", name: "Manager"},{id: "10", name: "Remarketing Manager"}]}}},{company: "CompanyName",id: "2",userProfile: {id: "3",telephone: "",user: {email: "some_email",firstName: "Firstname",lastName: "Lastname",groups: [{id: "1", name: "Seller"}]}}},{company: "CompanyName",id: "3",userProfile: {id: "4",telephone: "",user: {email: "some_email",firstName: "Firstname",lastName: "Lastname",groups: [{id: "2", name: "Manager"}]}}}]

let final = data.reduce((op,{userProfile:{user:{groups}}}) => {
  groups.forEach(({name}) => {
    op[name] = op[name] || 0
    op[name]++
  })
  return op
},{})

console.log(final)

Comments

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.