Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Chaining Array Methods 3:37
- Method Chaining 1 objective
- Working with Objects in Arrays 7:40
- Working with Objects 1 objective
- Combining filter() and map() 4:13
- Combining filter() and map() 1 objective
- Combining filter() and reduce() 4:42
- Combining filter() and reduce() 1 objective
- Nested Data and Additional Exploration 7:25
- Array Flattening 1 objective
- Nested Data 1 objective
Well done!
You have completed JavaScript Array Iteration Methods!

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Combining filter() with reduce(), you can remove values from an array, using the results to compute some value. Get some practice with this combination.
Snippets from the Video
const products = [
{ name: 'hard drive', price: 59.99 },
{ name: 'lighbulbs', price: 2.59 },
{ name: 'paper towels', price: 6.99 },
{ name: 'flatscreen monitor', price: 159.99 },
{ name: 'cable ties', price: 19.99 },
{ name: 'ballpoint pens', price: 4.49 }
];
// Result: { name: 'paper towels', price: 6.99 }
const products = [
{ name: 'hard drive', price: 59.99 },
{ name: 'lighbulbs', price: 2.59 },
{ name: 'paper towels', price: 6.99 },
{ name: 'flatscreen monitor', price: 159.99 },
{ name: 'cable ties', price: 19.99 },
{ name: 'ballpoint pens', price: 4.49 }
];
// Result: 239.97
Resources
Creating an initial value for reduce
(Spoiler Alert! Continue reading only if you've watched the whole video!)
The following solution is NOT RECOMMENDED, but will hopefully offer some insight into how the reduce method works. To create an initial value for the first problem, I would have had to construct a dummy object with a price
property set to zero, so the highest
parameter would have had a price
property to read in the if
statement.
const product = products
.filter(product => product.price < 10)
.reduce((highest, product) => {
if (highest.price > product.price) {
return highest;
}
return product;
}, { price: 0 });
Someone else trying to understand my code might have been confused as to why a dummy object was there, and what purpose it served. In this case, it makes more sense to let reduce
initialize the highest
parameter with the first product from the array.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Samuel Kleos
Front End Web Development Techdegree Student 13,728 Points1 Answer
-
Elliott Tuan
7,790 PointsWhy can't I return the highest price with teacher's method in video?
Posted by Elliott TuanElliott Tuan
7,790 Points1 Answer
-
Istvan Toth
14,348 Points1 Answer
-
Swan The Human
Full Stack JavaScript Techdegree Student 19,338 Points1 Answer
-
chelseacheevers
13,786 Points2 Answers
-
Taylor Hall
Front End Web Development Techdegree Graduate 14,496 Points1 Answer
-
Kaung Khant Zaw
1,515 Points1 Answer
-
gerardo keys
14,292 Points1 Answer
-
yoav green
8,611 Pointswhy do we need to "return item" in order to get { name: 'paper towels', price: 6.99 } eventually
Posted by yoav greenyoav green
8,611 Points1 Answer
-
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 Points0 Answers
-
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points3 Answers
-
Madeline Yao
Full Stack JavaScript Techdegree Student 9,611 Points2 Answers
-
Tomas Svojanovsky
26,680 Points3 Answers
-
Ivan Molina
2,870 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
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