Link to home
Start Free TrialLog in
Avatar of jblayney
jblayneyFlag for Canada

asked on

combine total value of array

hello,

I have this code:

let daysOfWeek = {
 "Mon": 5,
  "Tues": 2,
  "Friday": 3
}

Open in new window


How do i console.log the toal value of daysOfWeek?
ASKER CERTIFIED SOLUTION
Avatar of Daniel Pineault
Daniel Pineault

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jblayney

ASKER

thank you
The modern way is to reduce

https://jsfiddle.net/mplungjan/enjwbo5h/

const total = Object.values(daysOfWeek).reduce((a,b) => a+b)

Open in new window


Note: The for..in is more descriptive if you use day

let total = 0;
for (let day in daysOfWeek) {
  total += daysOfWeek[day];
}

Open in new window



PS: JAVA is not JavaScript
Avatar of Daniel Pineault
Daniel Pineault

Excellent answer Michel.
Thanks, Daniel