Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React, using .map with index

I have a simple React component like so:

import React from 'react';  const ListPage = ({todos}) => (   <div>     <h6>todos</h6>     <ul>     {todos.map(({todo, index}) => (       <li key={index}>{todo}</li>     ))}     </ul>   </div> )  ListPage.propTypes = {   todos: React.PropTypes.array, };  export default ListPage; 

I can see that the docs for Array.prototype.map() shows that the second argument is index, next to currentValue. How does one alter the existing code to pick up the second argument?

like image 510
ilrein Avatar asked Apr 06 '16 03:04

ilrein


People also ask

How do I map an index in React?

To use the map() method with index in React: Call the map() method on the array. The function you pass to the map() method gets called with the element and index.

What does .map do in React?

In React, the map method is used to traverse and display a list of similar objects of a component. A map is not a feature of React. Instead, it is the standard JavaScript function that could be called on an array. The map() method creates a new array by calling a provided function on every element in the calling array.

How do I map a map within a function in Reactjs?

To use a map() inside a map() function in React: Call map() on the outer array, passing it a function. On each iteration, call the map() method on the other array.

How do you find the index on a map?

To access the index of the array map() method, use the second argument of the callback function. The array map() method creates the new array populated with the results of calling the provided function on every item in the array.


2 Answers

You need to to this:

todos.map((key, index) => { ... }) without object brackets for arguments.

({todo, index}) => { ... } - that syntax means that you want to get properties todo and index from first argument of function.

You can see syntax here:

Basic syntax

(param1, param2, …, paramN) => { statements } (param1, param2, …, paramN) => expression          // equivalent to:  => { return expression; }  // Parentheses are optional when there's only one parameter: (singleParam) => { statements } singleParam => { statements }  // A function with no parameters requires parentheses: () => { statements } 

Advanced syntax

// Parenthesize the body to return an object literal expression: params => ({foo: bar})  // Rest parameters and default parameters are supported (param1, param2, ...rest) => { statements } (param1 = defaultValue1, param2, …, paramN = defaultValueN) => { statements }  // Destructuring within the parameter list is also supported var f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c; f();  // 6 
like image 167
Timur Bilalov Avatar answered Sep 30 '22 14:09

Timur Bilalov


Here is a example base on @Timur Bilalov answer to make easy to understand

var materials = [   'Hydrogen',   'Helium',   'Lithium',   'Beryllium' ];  materials.map((material,index) => console.log(index +" = " + material + " = " + materials[index])); 

Output

"0 = Hydrogen = Hydrogen" "1 = Helium = Helium" "2 = Lithium = Lithium" "3 = Beryllium = Beryllium" 

Reference
https://reactjs.org/docs/lists-and-keys.html https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Hope it help

like image 21
Linh Avatar answered Sep 30 '22 12:09

Linh