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

161
Views
How to get value from object in react

I have an object list data like as

data:

[
  {
    "postId": 1,
    "id": 1,
    "name": "id labore ex et quam laborum",
    "email": "Eliseo@gardner.biz",
    "body": "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium"
  },
  {
    "postId": 1,
    "id": 2,
    "name": "quo vero reiciendis velit similique earum",
    "email": "Jayne_Kuhic@sydney.com",
    "body": "est natus enim nihil est dolore omnis voluptatem numquam\net omnis occaecati quod ullam at\nvoluptatem error expedita pariatur\nnihil sint nostrum voluptatem reiciendis et"
  }
]

I want to get name value from data.

code:

return(
        <Fragment>
            
            {Object.keys(cardData).map((keys)=>{
                <li>{cardData[keys].name}</li>
                
            })}
            
        </Fragment>
    ) 

but I don't get any data in return. When I use map to iterate it. An error shows me cardData.map is not a function. please help me for solve the problem

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Couple of problems:

  1. You need to explicitly add a return statement in the callback function of the map() method

    return <li>{cardData[keys].name}</li>
    
  2. cardData seems to be an array. If that's the case, then no need to use Object.keys(...). Call map() method directly on the array

    {cardData.map((obj) => {
       return <li>{obj.name}</li>      
    })}
    

    Alternate option is to remove the curly brackets. This will allow you to remove the return keyword and the li will be returned implicitly

    {cardData.map((obj) => <li>{obj.name}</li>)}
    

Note: Don't forget to add the key prop on the li element:

{cardData.map((obj) => (
   <li key={obj.id}>{obj.name}</li>
))}

Edit

If cardData is not an array, and is just an object of the following form:

{
    "postId": 1,
    "id": 1,
    "name": "id labore ex et quam laborum",
    ...
}

then use the following code:

{Object.keys(cardData).map(key => {
    return <li>{cardData[key].name}</li>      
})}

OR use the implicit return by removing the curly brackets:

{Object.keys(cardData).map(key => ( 
    <li>{cardData[key].name}</li>      
))}
about 4 years ago · Juan Pablo Isaza Report

0

You have to explicitly return values from map.

Since cardData is already an array so you can use map directly on arrays, no need to take the keys and then process them to get the name.

CODESANDBOX

<>
  <ul>
    {cardData.map((o) => {
      return <li key={o.id}>{o.name}</li>;
    })}
  </ul>
</>
about 4 years ago · Juan Pablo Isaza Report

0

Works like a charm !

export default function App() {
  var cardData = [
    {
      "postId": 1,
      "id": 1,
      "name": "id labore ex et quam laborum",
      "email": "Eliseo@gardner.biz",
      "body": "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium"
    },
    {
      "postId": 1,
      "id": 2,
      "name": "quo vero reiciendis velit similique earum",
      "email": "Jayne_Kuhic@sydney.com",
      "body": "est natus enim nihil est dolore omnis voluptatem numquam\net omnis occaecati quod ullam at\nvoluptatem error expedita pariatur\nnihil sint nostrum voluptatem reiciendis et"
    }
  ];
  return (
    <div className="App">
        {Object.keys(cardData).map((keys)=> {return <li>{cardData[keys].name}</li>})}
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza 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!