如何使用lodash计数对象数组

时间:2019-04-26 07:50:59

标签: javascript lodash

我有如下对象数组:

[
    {
        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"}
                ]
            }
        }
    }
]

我想用group name来计数。

因此,我想要的结果是:

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

我尝试了 lodash countBy,如下所示:

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

但这不起作用。

Here is the fiddle.

2 个答案:

答案 0 :(得分:3)

_.flatMap()展开组数组,然后按名称计数:

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>

答案 1 :(得分:1)

您可以使用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)