Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed JavaScript Array Iteration Methods!
You have completed JavaScript Array Iteration Methods!
Preview
Use the reduce method to return a single value from an array of values.
Snippets from the Video
const prices = [6.75, 3.10, 4.00, 8.12]; // Total: 21.97
let total = 0;
prices.forEach(price => {
total += price;
});
console.log(total);
const names = ['Gary', 'Pasan', 'Gabe', 'Treasure', 'Gengis', 'Gladys', 'Tony'];
// Result: 4
MDN Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Sometimes you need to analyze all
of the elements in an array and
0:00
return just a single value,
like a number, string, or object.
0:00
For example,
you might have an array of prices and
0:04
you wanna get a single value,
the total of all the prices.
0:07
With the reduce method,
0:10
you can use the elements of an array to
compute and return any value you want.
0:12
The reduce method lets you turn all
the items in an array into one value.
0:17
If you had an array of names, for example,
and you wanted to find out how many names
0:22
there were with a length greater than six,
you could use reduce to return that total.
0:27
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up