Navigate back to the homepage

how to get key and value from json array of object in react native?

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

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To get keys and values from a JSON array of objects in react native, use the map() to iterate and Object.keys() to get the keys of an object using both methods we are able to iterate and use keys and values of JSON array of objects.

Today, I’m going to show How do I get keys and values from a JSON array of objects in react native, here I will use the javascript map() and Object.keys() method to get keys and values from JSON array of objects.

Let’s start today’s tutorial How do you get keys and values from a JSON array of objects in react native?

Example in React Native

In the following example, we are going to do

  1. Take example JSON array of objects
  2. apply the map() method to iterate the array.
  3. apply Object.keys() to get the key and value both from the object
  4. Print on react native app 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 = [
9 { id: 1, name: 'infinitbility.com' },
10 { id: 2, name: 'aguidehub.com' },
11 { id: 2, name: 'sortoutcode.com' },
12 ];
13 setDomains(domains);
14 }, []);
15
16 return (
17 <View style={styles.container}>
18 {domains.map((user) => {
19 let keys = Object.keys(user);
20 return (
21 <Text style={styles.paragraph}>{keys.map((key) => {
22 return (
23 <Text>{`Key ${key}, value ${user[key]} \n`}</Text>
24 )
25 })}</Text>
26 )
27 })}
28
29 </View>
30 );
31}
32
33const styles = StyleSheet.create({
34 container: {
35 flex: 1,
36 justifyContent: 'center',
37 backgroundColor: '#ecf0f1',
38 padding: 8,
39 },
40 paragraph: {
41 margin: 24,
42 fontSize: 18,
43 fontWeight: 'bold',
44 textAlign: 'center',
45 },
46});

In the above program, we used the first map() method to iterate and the second map() method to iterate object keys it help us to get keys and values both from objects.

let’s check the output.

React Native, Example of get key and value from json array of objects
React Native, Example of get key and value from json array of objects

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 remove the last character from a string in react native?

Example of to remove the last character from a string in react native with step by step guide.

June 18th, 2022 · 1 min read

how to get number of days between two dates in moment js?

Example of get number of days between two dates using moment js with step by step guide.

June 17th, 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]