Navigate back to the homepage

how to convert json object to array in react native?

Infinitbility
React Native
June 19th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To convert the object to an array in react native, just use Object.entries() to iterate the object and use the map() method to create an array of objects.

Basically, the Object.entries() method makes object key-value pair to an array of array, where every array has a key and value as an element of the array. like the following example.

1const x = { foo: 11, bar: 42 };
2console.log(Object.entries(x));
3// Output
4// [['foo', 11], ['bar', 42]]

After that, we will use the map() method to create desired structure of the array.

Today, I’m going to show you How do i convert a JSON object to an array in react native, as above mentioned here, I’m going to use the Object.entries() and map() methods to convert object to array.

Let’s start today’s tutorial how do you convert JSON object to array in react native?

In the above example, we will do

  1. Create a sample object variable.
  2. Use Object.entries() and map() method
  3. Print the Output on the screen
1import React, { useState, useEffect } from 'react';
2import { Text, View, StyleSheet } from 'react-native';
3
4export default function App() {
5 const [domains, setDomains] = useState('');
6
7 useEffect(() => {
8 let domains = { id: 1, name: 'infinitbility.com' };
9 const result = Object.entries(domains).map(([key, val]) => ({[key]: val}));
10 console.log(result);
11 setDomains(JSON.stringify(result));
12 }, []);
13
14 return (
15 <View style={styles.container}>
16 {domains}
17 </View>
18 );
19}
20
21const styles = StyleSheet.create({
22 container: {
23 flex: 1,
24 justifyContent: 'center',
25 backgroundColor: '#ecf0f1',
26 padding: 8,
27 },
28 paragraph: {
29 margin: 24,
30 fontSize: 18,
31 fontWeight: 'bold',
32 textAlign: 'center',
33 },
34});

As above mentioned, we are taken the example of the domain object and converted it into an array of objects.

Let’s check the output.

React Native, convert json object to array
React Native, convert json object to array
  • Try it yourself

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to convert string to int in react native?

Example of to convert string to int react native with step by step guide.

June 19th, 2022 · 1 min read

how to solve element type is invalid expected a string in react native?

Example of fix element type is invalid expected a string in react native with step by step guide.

June 19th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]