How to Get Length of Array in React JS?

Sep 28, 2022 . Admin

Hey Guys,

In this comprehensive tutorial, we will learn how to get length of array in react js. This example will help you how to get object length in react js. you can see get length of array react js. In this article, we will implement a react js array length.

In this example, we will create one "users" array and then i will simply use "length" property to get array size in react js app.

So, let's see follow the below step to create simple example.

Step 1: Create React JS App

In this step, open your terminal and execute the following command on your terminal to create a new react app:

npx create-react-app react-app-example
Step 2: Update App.js Component

Here, we will call two apis for getting posts and another for deleting post using axios request.

src/App.js
import './App.css';

function App() {

  /*------------------------------------------
  --------------------------------------------
  Users Lists
  --------------------------------------------
  --------------------------------------------*/
  const users = [
    {id: 1, name: 'Hardik', country: 'India'},
    {id: 2, name: 'Vimal', country: 'India'},
    {id: 3, name: 'Harshad', country: 'Canada'},
    {id: 4, name: 'Keval', country: 'Denmark'},
    {id: 5, name: 'Savan', country: 'USA'},
  ];

  return (
    <div className="App">
      <h1>How to Get Length of Array in React JS? - MyWebtuts.com</h1>
      <h2>Your users array length is (users.length): </h2>
      <h2>{ users.length }</h2>
    </div>
  );
}

export default App;
Step 3: Run React JS App

In the last step run your project using bellow command.

npm start
Output

I hope it can help you...

#React JS