Warning: session_start(): open(/tmp/sess_70eb6deb6b8177211ba8353818893051, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Using The Map() Method With Index In React - LearnShareIT

Using The Map() Method With Index In React

Using the map() method with index in React

The map() method in React will be very important because it is a method of the array. So how can we use map() method with index in React? Let’s read this article to find the best answer.

Using the map() method with index in React

How does the map() method work

The map() method will return a new array that each element creates based on the code logic we execute in the callback for each element loop.

Syntax:

Array.map(callback(element, index, array), thisArg)

Parameters:

  • callback: The function called for each time loop through an element.
  • element: Indicate the current element.
  • index: Indicate the index of the current element.
  • thisArg: An object to which can this keyword can refer. It can use as this keyword.

Example:

import { useState } from 'react';

function App() {
    const oldArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    const newArray = oldArray.map((ele, index) => { return ele + 1 });
    console.table(newArray);
    return (
        <div className="App">
        </div>
    );
}

export default App;
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';

ReactDOM.createRoot(document.getElementById('root')).render(
    <React.StrictMode>
      	<App />
    </React.StrictMode>
)

Output:

(index)Value
02
13
24
35
46
57
68
79
810
911
  • Array(10)
    • 0: 2
    • 1: 3
    • 2: 4
    • 3: 5
    • 4: 6
    • 5: 7
    • 6: 8
    • 7: 9
    • 8: 10
    • 9: 11
    • length: 10
    • [[Prototype]]: Array(0)

So here, use the map() method and it returns a newArray array which contains the element from oldArray plus 1.

You can also apply the map() method in this way

Example:

import { useState } from 'react';

function App() {
    const [animalList, setAnimalList] = useState([
        {name: 'Atlantic Spotted Dolphins', type: 'A'},
        {name: 'Bactrian Camels', type: 'B'},
        {name: 'Black Necked Stilt', type: 'B'},
        {name: 'Galapagos Flamingo', type: 'G'},
        {name: 'Galapagos Gecko', type: 'G'},
        {name: 'Bactrian Camels', type: 'B'},
        {name: 'Sheep', type: 'S'}
    ]);

    console.log(animalList);
    const showAnimal = (animalList.map((ele) => {
      	return (
        	<>
                <h5>Animal: {ele.name}</h5>
                <h6>Type: {ele.type}</h6>
            </>
      	)
    }));
  
    return (
      	<>
            <h1>Hello From Learn Share IT</h1>
            {showAnimal}
        </>
    );
}

export default App;
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';

ReactDOM.createRoot(document.getElementById('root')).render(
    <React.StrictMode>
    	<App />
    </React.StrictMode>
)

In this example, I create an animalList array containing a few objects with name and type properties. Then I use the map() method to loop over all elements in animalList array. Then for each of them, I return the Node element that shows the name and type properties of the object.

Summary

In this article, I’ve shown you some methods to use the map() method with index in React. And I have also shown you an example of how you can use the map() in real situation. I hope you like it. Thanks for your reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *