简体   繁体   中英

How to count in array of objects using lodash

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.

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> 

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM