- 01Setting Up (What You'll Need)
- 02Creating Variables
- 03Conditionals (if Statements)
- 04Loops (for Statements)
- 05Creating Functions
- 06Working with Arrays
- 07Working with Objects
- 08Loops (while Statements)
- 09Working with Strings
- 10Using Classes (Object-Oriented Programming)
- 11Error Handling (try...catch)
- 12Destructuring and the Spread Syntax
- 13Async Code (Promise / async & await)
- 14Advanced Array Methods (filter and reduce)
- 15The switch Statement
- 16The Ternary Operator
- 17Inheritance (Extending Classes)
- 18How to Write Comments
- 19Logical Operators (AND, OR, NOT)
- 20Constants (Making Read-Only Values with const)
- 21Splitting and Joining Strings (split and join)
- 22Null-Safe Syntax (?? and ?.)
- 23Searching Arrays and Collections (includes and find)
- 24Transforming Arrays with map
- 25Two-Dimensional Arrays (Table-Shaped Data)
- 26Building a Custom Error Class
- 27Default Arguments (Setting Initial Values for Parameters)
- 28Using Set (Collections)
- 29Verifying Correctness with assert (Your First Step Into Testing)
- 30Higher-Order Functions (Passing a Function as an Argument)
- 31Stacks and Queues (Basic Data Structures)
- 32The Basics of Type Conversion (Casting)
- 33Introduction to Regular Expressions (Pattern Matching)
- 34The Binary Search Algorithm
- 35Building a Caesar Cipher (a Letter-Shifting Cipher)
- 36Understanding How Bubble Sort Works
- 37Building and Displaying Dates (Basic Year/Month/Day Operations)
- 38Writing Several Unit Tests Together (Multiple Test Cases)
- 39Speeding Up Calculations with Memoization (Caching)
- 40Normalizing Strings (trim and Case Unification)
- 41The Difference Between Shallow Copy and Deep Copy
- 42The Basics of Enums (Enumerated Types)
- 43Flattening Arrays (flatten)
- 44Reversing a String and Checking for a Palindrome
- 45Pairing Up Two Arrays (a zip Operation)
- 46Rounding Numbers (floor, ceil, and round)
- 47Multi-Line Strings (Template Literals)
- 48Returning Multiple Values from a Function (Array Destructuring)
- 49Finding the GCD and LCM (the Euclidean Algorithm)
- 50Formatting Numbers (Digit Alignment and Decimal Precision)
- 51Cleanup Processing with try/catch/finally
- 52Writing Type-Agnostic, General-Purpose Functions
- 53The Basics of Map (an Object for Key-Value Pairs)
- 54Generating Random Numbers
- 55Bitwise Operations (AND, OR, XOR, and Shift Operations)
- 56Using static (Static Class Properties and Methods)
- 57Waiting a Fixed Amount of Time (setTimeout and await)
- 58Watch Out for Floating-Point Rounding Error
- 59Transforming and Flattening at Once with flatMap()
- 60FizzBuzz (the Classic Practice Problem)
- 61Checking Whether a Number Is Prime
- 62Set Operations with Set (Union, Intersection, and Difference)
- 63Converting Number Bases (Binary and Hexadecimal)
- 64Checking That Brackets Match (an Application of Stacks)
- 65Checking for an Anagram
- 66Checking Whether a Year Is a Leap Year
- 67Converting Temperature (Celsius ⇄ Fahrenheit)
- 68Finding the Prime Factorization
- 69[Applied] Build a Simple To-Do List Tool
Advanced Array Methods (filter and reduce)
In this lesson you'll master array's .filter() and .reduce() so you can extract matching data and aggregate it in short, readable code. This is for people searching "JavaScript filter reduce usage" or "JavaScript array aggregation."
.filter(condition) builds a new array containing only the elements that match a condition. .reduce((accumulator, element) => { ... }, initialValue) is a slightly more advanced but extremely useful method for collapsing an entire array into a single value (like a sum). Compared to writing the processing one element at a time in a for loop, using these methods makes "what you're trying to do" show up directly in the code, keeping it readable.
The sample code extracts only the scores that are 70 or above from an array of test scores using .filter((s) => s >= 70), and calculates everyone's total with .reduce((sum, s) => sum + s, 0). The second argument to reduce (0 in this case) is the starting value for the accumulation — you'd use 0 for a sum, 1 for a product, and so on, depending on what you're calculating.
A common beginner stumbling block is the order and role of .reduce()'s callback function arguments. It's easy to mix up "the accumulated value so far" (first argument) with "the element currently being processed" (second argument) — at first, it's a good idea to log each step to the console one at a time to check how it behaves. Also remember: if you forget to specify an initial value, the array's first element gets treated as the initial value instead.
In real development, combining filter and reduce is a classic pattern for data aggregation — extracting only orders that meet certain criteria from an order history, or calculating the total price of items in a cart. It's a combination worth remembering, since it shows up constantly in real-world logic like data analysis and sales aggregation.
.filter() and .reduce() can also be chained together, so you can write filtering and aggregation in a single line, like scores.filter((s) => s >= 70).reduce((sum, s) => sum + s, 0).
Similar aggregation processing is often combined with .map() too, and it's very common in real work to write a whole data-processing pipeline as one flow: "extract data matching a condition, transform it into just the information you need, then finally sum it up."
💡 Anything passed to console.log() appears in the output below.
