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>      
))}
Answer from Yousaf on Stack Overflow
🌐
Medium
medium.com › @ismailtaufiq19 › display-objects-key-value-pairs-in-reactjs-95d8a26bd74b
Display Object’s Key-Value Pairs in ReactJS - Taufiq Ismail - Medium
August 25, 2023 - Mostly, to display the value of an object in ReactJS, you just need to call the name of the object followed by the key of the value to be displayed.
Discussions

REACT: Render the keys of only one object
So I have an object of objects, and I’m trying to render only the keys from the first object. I’ll post the full codepen at the end, but so far I’ve got: · Object.keys(this.props.data).filter((obj) => Object.keys(this.props.data).indexOf(obj) == 0) · Is being used to isolate the first ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
2
0
August 26, 2018
Extract keys alongside values from object
I do think Object.entries is what you want... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries It returns a "tuple" of [key, value]. So in your example you would do: if (this.state.cartItems) { const items = Object.entries(this.state.cartItems) .map(([key, value]) => ( More on reddit.com
🌐 r/reactjs
6
1
May 24, 2019
react js get value from object based on key name [closed]
my return object looks like this. I would like to extract the value if code==="targetDate". Could someone please help. More on stackoverflow.com
🌐 stackoverflow.com
reactjs - Accessing key name in key-value pairs on props object in React - Stack Overflow
I'm working on a React app and want the component I'm working on to be able to access the key names in the props object the component is receiving. For example, I have this object: var fido = { ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Pluralsight
pluralsight.com › blog › tech guides & tutorials
Accessing Data through Props with Known Key Names in React.js | Pluralsight
August 6, 2020 - You just need to make use of the object name followed by the key name to access the key-value pair of the state object. Previously, you have accessed the state object’s key-value pair, but the props object can also be accessed the same way.
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
REACT: Render the keys of only one object - Curriculum Help - The freeCodeCamp Forum
August 26, 2018 - So I have an object of objects, and I’m trying to render only the keys from the first object. I’ll post the full codepen at the end, but so far I’ve got: renderKeys = () => { return Object.keys(this.props.data).filter…
🌐
Reddit
reddit.com › r/reactjs › extract keys alongside values from object
r/reactjs on Reddit: Extract keys alongside values from object
May 24, 2019 -

I have an object with nested objects in db that I receive in my app.

if (this.state.cartItems) {

const cartItems = Object.entries(this.state.cartItems);

storeItem = cartItems.map((cartItem, index) => (

<StoreItem
imgUrl={cartItem.imgUrl}
key={index}
brand={cartItem.brand}
description={cartItem.description}
price={cartItem.price}
/>

));

}

I want to make a delete button, and for that I need the key of specific object. How can I extract that key and send it alongside other properties?

Find elsewhere
🌐
Reddit
reddit.com › r/reactjs › using object.keys() to turn a data object into an array, mapping the array, and passing the values as props.
r/reactjs on Reddit: Using Object.keys() to turn a data object into an array, mapping the array, and passing the values as props.
March 19, 2016 -

Right now I am making an app using the twitch api. There channels endpoint returns a data object. Since it's an object I can't map over it like normally I would if it was an array.

Thus why I am using Objects.keys(myObj) to turn the object into an array, however it's only an array of it's keys, and in order to get an array of the keys and values I have to do something like this

var myObj = {
  name: 'zack',
  age: 27,
  height: 511
};

var newArr = Object.keys(myObj);
console.log(newArr);

var mappedArr = newArr.map(function(i) {
  return [i, myObj[i]];
});

console.log(mappedArr);

the last console log is an array of my key:value pairs, but each key/value pair is in its own array.

I just want to take the object data, map through it, and pass certain parts of the objects data to a child component as props like this: https://gist.github.com/laere/eaf0fa4eb90b731f92e9

How can I achieve the same thing? Here is my code: https://gist.github.com/laere/ac142dedd253087ffda6

UPDATE I solved the issue. i realized I was being mentally retarded and not just calling the values within my component like this {myObj.name} or {myObj.whatevervalueithas}. I was overcomplicating it thinking I had to map over the data like my olther components. What a day.

🌐
freeCodeCamp
forum.freecodecamp.org › t › react-how-to-access-object-within-object › 360319
React - How to access Object within Object? - The freeCodeCamp Forum
March 18, 2020 - I have the current JSON object, named ‘data’. It has the following form: { "1": { "A" { "a": 2 } } } I’m not sure of how I’m supposed to access the 2. I’ve tried everything. The errors keep coming back as undefined. All I need to do is access whatever value is attached to "a". I’ve ...
🌐
Webtips
webtips.dev › display-key-value-pairs-in-react
How to Display Key-Value Pairs in React in a Table - Webtips
May 19, 2023 - This component expects an obj prop that we can use inside the table to display its keys and values. To use it anywhere, simply import the component and pass the object you want to display as the obj prop: ... import React from 'react' import KeyValueTable from './KeyValueTable' const user = { "id": 1, "name": "Leanne Graham", "username": "Bret", "email": "Leanne-Bret@", "phone": "1-770-736-8031 x56442" } const App = () => ( <KeyValueTable obj={user} /> ) export default App App.jsx
Top answer
1 of 1
6

What you need here is recursion.

Though, there is something missing in your design, you are not defining the exact condition for when should the program actually extract the value.

For example, lets define that if a value of a given key is a string, then we will render it to the page.

Such condition will look something like this:

if (typeof data[key] === 'string' || data[key] instanceof String)

Then with this decision, you can write a function that will take a data object and recursively will go over the object's keys, based on the condition above, it will return the value Or another call to the same function with the value as the new data object.

The function will look something along this block of code:

function generateData(data) {
  const newData = Object.keys(data).reduce((result, currentKey) => {
    if (typeof data[currentKey] === 'string' || data[currentKey] instanceof String) {
      result.push(currentKey);
    } else {
      const nested = generateData(data[currentKey]);
      result.push(...nested);
    }
    return result;
  }, []);
  return newData;
}

I'm using .reduce here to build a new array. and as mentioned above, i will push to the new array either the key or the result of a recursive call to the function with the value as the new data object, of course based on the condition mentioned above.
Note, that the recursive call will return an array, so i'm using the spread syntax to extract it's members.

Now, we want our key and value to get rendered to the DOM.
So instead to just push the key or value, we can push an element.

Here is a full running example:

const myData = {
  "key1": "value1",
  "key2": "value2",
  "key3": "value3",
  "key4": {
    "k1": "val1",
    "k2": {
      "p1": "v1",
      "p2": "v2",
      "p3": "v3"
    }
  }
}

const generateElement = (key,value) => {
  return (
    <div key={key} className="row">
      <div className="col-xs-6 ins-label">{key}</div>
      <div className="col-xs-6">{value}</div>
    </div>
  );
}

function generateData(data) {
  const newData = Object.keys(data).reduce((result, currentKey) => {
    if (typeof data[currentKey] === 'string' || data[currentKey] instanceof String) {
      const elementToPush = generateElement(currentKey, data[currentKey]);
      result.push(elementToPush);
    } else {
      const nested = generateData(data[currentKey]);
      result.push(...nested);
    }
    return result;
  }, []);
  return newData;
}

class App extends React.Component {
  render() {
  const { data } = this.props;
    return (
      <div>
        {generateData(data)}        
      </div>
    )
  }
}

ReactDOM.render(<App data={myData} />, document.getElementById('root'));
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="root"></div>

🌐
Stack Overflow
stackoverflow.com › questions › 50815667 › react-get-value-og-key-from-array
React Get Value og Key from Array
const response = [ { ... }, { "id":"2", "name":"bcdx" } ] } } ] } ]; response[0].new_data.map((value, key) => { for (var k in value) { console.log(value[k]); } }) ... Find the answer to your question by ...