How to Get Length of Array in React Native?

Aug 05, 2022 . Admin

Hi Dev,

This tutorial will give you an example of how to get the length of the array into react native. you'll learn how to get object length in react native. I would like to show you get the length of array react native. let’s discuss react native array length. Let's see below an example of how to find array length in react native.

If you created an array of objects and you need to find the length on the array of objects in react native app then you can do it using ".length" option. in this example, I created user array objects and get the length of the array.

So, let's start following example:

Step 1: Download Project

This is optional; however, if you have not created the React Native app, then you may go ahead and execute the below command:

expo init ExampleApp
Step 2: Update App.js

In this step, You will open the App.js file and put the code.

App.js
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';

export default 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 (
    
      
        users.length is {users.length}
      
    
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});
Step 3: Run Project

In the last step run your project using the below command.

expo start

You can QR code scan in Expo Go Application on mobile.

Output:

It will help you...

#React Native