Solved

Loop through object and sum totals

Posted on 2016-08-11
4
69 Views
Last Modified: 2016-08-12
Hello, I have some employee data in employeeGrid object which consists of employee, hours.

How can I loop through employeeGrid and sum up the hours for each employee. Once I get the list of employees and their hours I want to add that to a div .EmployeeHours.

With my code below, I just get a list of the employees and their hours with dupes and no sums.

Here is some sample data:

Dan, 8
Dan, 4
Dan, 2
Julie, 8
Mark, 8

The EmployeeHours should look like this:

Dan 14
Julie 8
Mark 8


function setEmployeeHours(employeeGrid){
       
        $('.EmployeeHours').html('');
       
        for (var key in employeeGrid) {

            var obj = employeeGrid[key];
            if (obj.employee) {
                $('.EmployeeHours').append(obj.employee+ " " + obj.hours + "</br>");
 }
        }
    }
0
Comment
Question by:Scott Baldridge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:James Bilous
ID: 41753058
You could use a map keyed on the employee names. As you loop through the employees in the grid, look up the name in the map and increment the value by the number of hours in that row. Finally, print the key value pairs of the map to get the total hours worked.
1
 
LVL 9

Expert Comment

by:Paweł
ID: 41753966
I made a lot of assumptions, but this should be what you're looking for

http://codepen.io/anon/pen/mEaYPE?editors=1010

unless you have an ordered list you'll have to do a nested for loop.


var employees = [{
  "employee": "Dan","hours": 8 }, {
  "employee": "Dan","hours": 4 }, {
  "employee": "Dan","hours": 2 }, {
  "employee": "Julie","hours": 8 }, {
  "employee": "Mark","hours": 8 }];

var result = [];

for (var i in employees) {
  var append = true;
  var emp = employees[i]

  if (result.length === 0) {
    result.push(emp);
    continue;
  }

  for (var x in result) {
    var pushedEmp = result[x];

    if (pushedEmp.employee === emp.employee) {
      pushedEmp.hours += emp.hours;
      append = false;
      break;
    }
  }

  if (append)
    result.push(emp);
}

for (var r in result) {
  var emp = result[r];
  var outputDiv = document.getElementById("output");
  var div = document.createElement("div");
  
  div.innerText = emp.employee + " " + emp.hours;
  outputDiv.appendChild(div);
}

Open in new window

0
 
LVL 8

Accepted Solution

by:
James Bilous earned 500 total points
ID: 41753998
@Pawel, a nested for loop is not required; this can be done much more simply with a map.

http://codepen.io/anon/pen/LkqEbJ

var employees = [{
  "employee": "Dan","hours": 8 }, {
  "employee": "Dan","hours": 4 }, {
  "employee": "Dan","hours": 2 }, {
  "employee": "Julie","hours": 8 }, {
  "employee": "Mark","hours": 8 }];

var employeeMap = {"Dan" : 0, "Julie" : 0, "Mark" : 0};

employees.forEach(function(item) {
  employeeMap[item["employee"]] += item["hours"]; 
});

for (var employee in employeeMap)
{
  results.innerHTML = results.innerHTML + employee + ": " + employeeMap[employee] + "<br/>";
}

Open in new window

0
 

Author Closing Comment

by:Scott Baldridge
ID: 41754155
Thanks much appreciated!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Can't get javascript video player button to work 10 23
Date on a table 16 36
window close link 7 35
Use Mid in Html 6 23
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question