Array Filter and Map in JavaScript

Level 3117 keystrokes

Method chaining allows combining filter and map operations on arrays.

JavaScript code

// Create an array of numbers
1const nums = [1, 2, 3, 4, 5, 6];
// Chain filter and map on nums
2const result = nums
// filter keeps only even numbers (n % 2 === 0): 2, 4, 6
3 .filter(n => n % 2 === 0)
// map doubles each remaining number: [4, 8, 12]
4 .map(n => n * 2);
// Print the final result
5console.log(result);

Output

[ 4, 8, 12 ]
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • =× 7
  • ,× 5
  • ;× 3
  • .× 3
  • (× 3
  • )× 3
  • >× 2
  • [× 1
  • ]× 1
  • *× 1

More JavaScript examples at level 3

← JavaScript typing practice