使用lodash / underscore分组并总结

时间:2016-07-28 12:48:22

标签: group-by sum underscore.js lodash

我有以下数组:

var data = [{ "Username": "rahul.prajapati", "Name": "Rahul Prajapati", "Projectkey": "project1", "Hours": 5, }, { "Username": "rahul.prajapati", "Name": "Rahul Prajapati", "Projectkey": "project1", "Hours": 7.5, }, { "Username": "rahul.prajapati", "Name": "Rahul Prajapati", "Projectkey": "project2", "Hours": 8, }, { "Username": "rahul.prajapati", "Name": "Rahul Prajapati", "Projectkey": "project2", "Hours": 7.5, }, { "Username": "kishor.thakkar", "Name": "Kishor Thakkar", "Projectkey": "project2", "Hours": 0.25, }, { "Username": "kishor.thakkar", "Name": "Kishor Thakkar", "Projectkey": "project2", "Hours": 0.25, }, { "Username": "kishor.thakkar", "Name": "Kishor Thakkar", "Projectkey": "project3", "Hours": 0.25, }, { "Username": "kishor.thakkar", "Name": "Kishor Thakkar", "Projectkey": "project3", "Hours": 1.5, } ]

我需要下面的数组,我需要项目应该按“username”分组,每个数组都有一个“Projects”数组,这个“Projects”数组将按应该添加“projectKey”和相同projectKey的小时数

[{
      "Name": "Rahul Prajapati",
      "Username" "rahul.prajapati",
      "PROJECTS":[
        {
          "ProjectKey" : "project1",
          "Hours" : 12.5
        },
        {
          "ProjectKey" : "project2",
          "Hours" : 15.5
        },
      ]
   },
   {
     "Name": "Kishor Thakkar",
     "Username" "kishor.thakkar",
     "PROJECTS":[
       {
         "ProjectKey" : "project1",
         "Hours" : 0.50
       },
       {
         "ProjectKey" : "project2",
         "Hours" : 1.75
       },
     ]
  }
 ]

是否可以通过Lodash。

请帮助,我是lodash的新手

每一个帮助都很明显

1 个答案:

答案 0 :(得分:2)

这是一个使用lodash的解决方案,它被分解为不同的函数,使其更具可读性(我希望):

// function used to calculate the total - _.sumBy not available in old lodash
var totalHours = function(total, project){
    return total + project.Hours;
}

// function to sum the hours for each project
var sumProjects = function(projects){
    return {
        Projectkey: projects[0].Projectkey,
        Hours: _.reduce(projects, totalHours, 0)
    }
}

// function to group the projects for each user
var groupProjects = function(projects){
    return _.chain(projects)
        .groupBy('Projectkey')
        .map(sumProjects)
        .value();
}

// first group by Username
var groups = _.groupBy(data, 'Username');

// now build what we want for each group
var result = _.map(groups, function(group){
    return {
        Name: group[0].Name,
        Username: group[0].Username,
        PROJECTS: groupProjects(group)
    };
})

var data = [{
      "Username": "rahul.prajapati",
      "Name": "Rahul Prajapati",
      "Projectkey": "project1",
      "Hours": 5,
    },
    {
      "Username": "rahul.prajapati",
      "Name": "Rahul Prajapati",
      "Projectkey": "project1",
      "Hours": 7.5,
    },
    {
      "Username": "rahul.prajapati",
      "Name": "Rahul Prajapati",
      "Projectkey": "project2",
      "Hours": 8,
    },
    {
      "Username": "rahul.prajapati",
      "Name": "Rahul Prajapati",
      "Projectkey": "project2",
      "Hours": 7.5,
    },
    {
      "Username": "kishor.thakkar",
      "Name": "Kishor Thakkar",
      "Projectkey": "project2",
      "Hours": 0.25,
    },
    {
      "Username": "kishor.thakkar",
      "Name": "Kishor Thakkar",
      "Projectkey": "project2",
      "Hours": 0.25,
    },
    {
      "Username": "kishor.thakkar",
      "Name": "Kishor Thakkar",
      "Projectkey": "project3",
      "Hours": 0.25,
    },
    {
      "Username": "kishor.thakkar",
      "Name": "Kishor Thakkar",
      "Projectkey": "project3",
      "Hours": 1.5,
    }
 ];

var groups = _.groupBy(data, 'Username');

var totalHours = function(total, project){
	return total + project.Hours;
}

var sumProjects = function(projects){
	return {
		Projectkey: projects[0].Projectkey,
		Hours: _.reduce(projects, totalHours, 0)
	}
}

var groupProjects = function(projects){
	return _.chain(projects)
		.groupBy('Projectkey')
		.map(sumProjects)
		.value();
}

var result = _.map(groups, function(group){
	return {
		Name: group[0].Name,
		Username: group[0].Username,
		PROJECTS: groupProjects(group)
	}
})

document.getElementById('result').textContent = JSON.stringify(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.0/lodash.min.js"></script>

<p>
  <pre id="result"></pre>
</p>