How To Use Array In React JS

Websolutionstuff | Aug-12-2022 | Categories : React JS

In this article, we will see how to use an array in React JS. We can use the JavaScript standard Array functions in React JS. We can see react loop array example in the render. Also, we are using the map() method. map() creates a new array from calling a function for every array element. map() calls a function once for each element in an array. map() does not execute the function for empty elements.

So, let's see how to access array elements in React JS or how to render an array of components React JS.

Iterate String Array:

we will see how to loop through the Array and display it in the UI.

Create a new String Array using the below code in your index.html file.

const myArray = ['Laravel', 'PHP', 'Node JS', 'React JS', 'jQuery'];

The Array contains the list of items. We can iterate and display the Array in the UI using map() function.

<!DOCTYPE html>
<html>
    <head>
        <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
        <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
        <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
    </head>
    <body>

        <div id="mydiv"></div>

        <script type="text/babel">      

          function Hello() {  
         
          const myArray = ['Laravel', 'PHP', 'Node JS', 'React JS', 'jQuery'];  
            
          return (  
            <div className="container">     
                <h1> How To Use Array In React JS - Websolutionstuff</h1>  
             
                {myArray.map(name => (  
                  <li>  
                    {name}  
                  </li>  
                ))}  
             
            </div>  
          );  
        }
          
        ReactDOM.render(<Hello />, document.getElementById('mydiv'))

        </script>
    </body>
</html>

Output:

array_map_example_react_js

 

 

Iterate Array Of Objects:

how to loop through the array of objects. It is similar to the String Array example.

<!DOCTYPE html>
<html>
    <head>
        <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
        <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
        <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
    </head>
    <body>

        <div id="mydiv"></div>

        <script type="text/babel">      

        function Hello() {  

            const tdstyle = {  
              border:"1px solid black",
              padding:"15px",
            };
       
            const myArray =  [  
                {  
                    'id': 1,   
                    'name': 'Laravel',
                    'website': 'https://laravel.com/'
                },  
                {  
                    'id': 2,   
                    'name': 'React JS',   
                    'website': 'https://reactjs.org/'
                },  
                {  
                    'id': 3,   
                    'name': 'jQuery',
                    'website': 'https://jquery.com/'
                },  
            ];  
            
            return (  
            <div className="container">     
                <h1> How To Use Array In React JS </h1>  
             
                <table style={{borderCollapse:"collapse"}}>  
                    <tr>  
                        <th>ID</th>  
                        <th>Name</th>  
                        <th>Website</th>  
                    </tr>  
            
                    {myArray.map((myArray, index) => (  
                      <tr data-index={index}>  
                        <td style={tdstyle}>{myArray.id}</td>  
                        <td style={tdstyle}>{myArray.name}</td>  
                        <td style={tdstyle}>{myArray.website}</td>  
                      </tr>  
                    ))}  
            
                </table>   
             
            </div>  
          );  
        }
          
        ReactDOM.render(<Hello />, document.getElementById('mydiv'))

        </script>
    </body>
</html>

Output:

array_of_objects_example_react_js

 


You might also like:

Recommended Post
Featured Post
How To Bind Data In React JS
How To Bind Data In React JS

In this article, we will see how to bind data in React JS. Also, we will see how to bind the variable value in the...

Read More

Aug-19-2022

Laravel 9 Authentication Using Inertia JS
Laravel 9 Authentication Using...

In this article, we will see laravel 9 authentication using inertia js. Here, you can learn how to authenticat...

Read More

Dec-05-2022

How to add Pagination in Laravel 11 Livewire
How to add Pagination in Larav...

Hello, laravel web developers! This article will show how to add pagination in laravel 11 Livewire. Here, we'll...

Read More

Jun-14-2024

How To Remove Spaces Using JQuery
How To Remove Spaces Using JQu...

In this article, we will explain to you how to remove extra space using jquery. many times we have requirements to...

Read More

Aug-10-2020