React Course: Arrays and Methods

React is one of the JavaScript libraries optimized for building user interfaces (UI). In many cases, working with arrays is essential when constructing UIs, and effectively using arrays and their associated methods in JavaScript is crucial. In this tutorial, we will explore various methods for handling arrays in React and how to utilize them.

1. Understanding Arrays in JavaScript

An array is a collection of ordered data. In JavaScript, arrays can be used to store and process multiple pieces of data. Arrays provide various methods that allow us to manipulate their contents.

1.1 Creating Arrays

There are several ways to create an array. The basic methods to create arrays are as follows.

const array1 = [];
const array2 = [1, 2, 3];
const array3 = new Array(4, 5, 6);

1.2 Array Properties

Arrays have several useful properties. Among them, the length property allows you to check the length of the array.

console.log(array2.length); // 3

2. Array Methods

Array methods provide various functionalities that are useful for working with arrays. Below, we will explain commonly used array methods.

2.1 push()

The push() method adds one or more elements to the end of an array.

const arr = [1, 2];
arr.push(3);
console.log(arr); // [1, 2, 3]

2.2 pop()

The pop() method removes the last element from an array and returns that element.

const arr = [1, 2, 3];
const lastElement = arr.pop();
console.log(lastElement); // 3
console.log(arr); // [1, 2]

2.3 shift()

The shift() method removes the first element from an array and returns that element.

const arr = [1, 2, 3];
const firstElement = arr.shift();
console.log(firstElement); // 1
console.log(arr); // [2, 3]

2.4 unshift()

The unshift() method adds one or more elements to the beginning of an array.

const arr = [2, 3];
arr.unshift(1);
console.log(arr); // [1, 2, 3]

2.5 splice()

The splice() method is used to add or remove elements from an array.

const arr = [1, 2, 3, 4];
arr.splice(2, 1, 'a', 'b'); 
console.log(arr); // [1, 2, 'a', 'b', 4]

2.6 slice()

The slice() method copies a portion of an array and returns a new array.

const arr = [1, 2, 3, 4];
const newArr = arr.slice(1, 3);
console.log(newArr); // [2, 3]

2.7 forEach()

The forEach() method executes a given function on each element of the array.

const arr = [1, 2, 3];
arr.forEach(element => {
    console.log(element); // 1, 2, 3
});

2.8 map()

The map() method returns a new array containing the results of calling a given function on every element in the array.

const arr = [1, 2, 3];
const doubled = arr.map(num => num * 2);
console.log(doubled); // [2, 4, 6]

2.9 filter()

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

const arr = [1, 2, 3, 4];
const evenNumbers = arr.filter(num => num % 2 === 0);
console.log(evenNumbers); // [2, 4]

2.10 reduce()

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

const arr = [1, 2, 3, 4];
const sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // 10

3. Utilizing Arrays in React

When developing React applications, arrays play a crucial role. They can be efficiently utilized to dynamically update user interfaces or process user inputs.

3.1 Managing Arrays as State

React components often manage arrays as state. Here’s an example of using an array as state.

import React, { useState } from 'react';

const App = () => {
    const [items, setItems] = useState(['Apple', 'Banana', 'Cherry']);

    const addItem = () => {
        setItems([...items, 'Strawberry']);
    };

    return (
        

Fruit List

    {items.map((item, index) => (
  • {item}
  • ))}
); }; export default App;

3.2 Conditional Rendering

Conditional rendering can be implemented using array methods. For example, rendering only elements that meet a specific condition can be done in various ways.

const App = () => {
    const items = ['Apple', 'Banana', 'Cherry', 'Strawberry'];

    return (
        

Fruit List

    {items.filter(item => item.startsWith('A')).map((item, index) => (
  • {item}
  • ))}
); };

3.3 Updating Elements of an Array

There are situations where only specific elements of an array need to be modified when updating state. In this case, map() can be used with setState.

const App = () => {
    const [items, setItems] = useState(['Apple', 'Banana', 'Cherry']);

    const updateItem = (index) => {
        const newItems = items.map((item, i) => (i === index ? 'Updated Fruit' : item));
        setItems(newItems);
    };

    return (
        

Fruit List

    {items.map((item, index) => (
  • {item}