JavaScript / JavaScript Arrays and Objects

Exploring Array Methods and Iteration

This tutorial explores the powerful array methods available in JavaScript, such as map, filter, and reduce. You will also learn how to iterate over arrays.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Explains how to work with arrays and objects to store and manipulate data effectively.

Introduction

The goal of this tutorial is to introduce you to some of the most powerful and commonly used array methods in JavaScript: map(), filter(), and reduce(). These methods are crucial to understand as they help in manipulating and managing data in JavaScript arrays efficiently.

After completing this tutorial, you will be able to:
- Understand and use map(), filter(), and reduce() methods.
- Iterate over arrays using these methods.
- Manipulate and manage data in arrays more efficiently.

This tutorial assumes you have basic understanding of JavaScript and its syntax.

Step-by-Step Guide

Array.map()

The map() method creates a new array with the results of calling a provided function on every element in the array.

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

In this example, we've used map() to multiply every element in the array by 2.

Array.filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

Here, filter() is used to get all words in the array with length greater than 6.

Array.reduce()

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in single output value.

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

In this example, reduce() is used to sum up all the elements of the array.

Code Examples

Using map()

const numbers = [1, 2, 3, 4, 5];

// We'll use map to square each number
const squares = numbers.map(number => number * number);

console.log(squares);
// expected output: [1, 4, 9, 16, 25]

Using filter()

const numbers = [1, 2, 3, 4, 5, 6];

// We'll use filter to get only even numbers
const evens = numbers.filter(number => number % 2 === 0);

console.log(evens);
// expected output: [2, 4, 6]

Using reduce()

const numbers = [1, 2, 3, 4, 5];

// We'll use reduce to get the product of all numbers
const product = numbers.reduce((total, number) => total * number, 1);

console.log(product);
// expected output: 120

Summary

In this tutorial, we've explored three powerful array methods: map(), filter(), and reduce(). These methods are crucial for efficiently manipulating and managing data in JavaScript arrays.

To continue learning, you can explore other array methods like find(), some(), every(), includes(), etc.

Practice Exercises

  1. Use the map() method to double each number in an array.
  2. Use the filter() method to get all numbers greater than 50 from an array.
  3. Use the reduce() method to find the maximum number in an array.

Solutions

// Exercise 1
const numbers = [10, 20, 30, 40, 50];
const doubles = numbers.map(number => number * 2);
console.log(doubles); // [20, 40, 60, 80, 100]

// Exercise 2
const numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
const greaterThanFifty = numbers.filter(number => number > 50);
console.log(greaterThanFifty); // [60, 70, 80, 90, 100]

// Exercise 3
const numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
const maxNumber = numbers.reduce((max, number) => Math.max(max, number), -Infinity);
console.log(maxNumber); // 100

Keep practicing to get more comfortable with these concepts!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Word to PDF Converter

Easily convert Word documents to PDFs.

Use tool

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

HTML Minifier & Formatter

Minify or beautify HTML code.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help