Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
Looping through JSON objects and converting them to React components

I have a JSON file with 3 different objects inside of them (card1, card2 and card3). I want to loop through these objects in JavaScript, turn them into an array of objects, then in my React code, I'm trying to turn them into Components depending on how many there are in the JSON file.

What I have so far:

import Card from "./card-elements/Card";
import json from "./data.json";

const list = [];
for (const key in json) {
    if (json.hasOwnProperty(key)) {
        list.push(key);
    }
}

function App() {
    return (
        <div className="card-area">
            {list.map(i => {
                return <Card title={i.title}
                             description={i.description}
                             borderColor={"red"}/>
            })}
        </div>
    );
}

export default App;

data.json:

{
    "card1": {
      "title": "Card One",
      "description": "This is card one's description. Straight from JSON file!"
    },
    "card2": {
      "title": "Card Two",
      "description": "Directly from the JSON file, this is card two's description"
    },
    "card3": {
      "title": "Card Three",
      "description": "And finally, this is the last cards description!"
    }
}

Current output: No errors occur with my React, it just only displays one Component and the "title" and "description" are not showing

Any help would be appreciated, thank you!

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

No need to push each item to a list and use it. Instead of use Object.entries.

Try like this

function App() {
    return (
        <div className="card-area">
            {(Object.entries(json) || []).map(([key, value]) => {
                return (
                    <Card
                        title={value.title}
                        description={value.description}
                        borderColor={"red"}
                    />
                );
            })}
        </div>
    );
}
about 4 years ago · Santiago Gelvez Report

0

In React, you should to use the useState hook to update a component.

Try something like:

import Card from "./card-elements/Card";
import json from "./data.json";


function App() {
    const [cardData, setCardData] = React.useState();

    React.useEffect(() => {
      const list = [];
      for (const key in json) {
       if (json.hasOwnProperty(key)) {
          list.push(key);
       }
      }
    }, [])

    return (
        <div className="card-area">
            {cardData && list.map(i => {
                return <Card title={i.title}
                             description={i.description}
                             borderColor={"red"}/>
            })}
        </div>
    );
}

export default App;

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!