Use Object.entries method:
const petList = Object.entries(fido).map(([key,value])=>{
return (
<div>{key} : {value.toString()}</div>
);
})
value.toString() for boolean types to render correctly
Again map through the inner elements present in the result[item].
return (
<React.Fragment>
{ Object.keys(result).map((item, i) => (
<div key={i} className="report">
{result[item].map((media,ind) =>
<div key={ind}>{media.name}</div>
)}
</div>
))}
</React.Fragment>
)
Try this code : Here is the link to the code on: https://codesandbox.io/s/pj1lv7y4xq
const arrayvals = [
{ Number: 1, newNumber: "1", name: "FB" },
{ Number: 3, newNumber: "2", name: "FB" },
{ Number: 7, newNumber: "5", name: "GK" },
{ Number: 8, newNumber: "4", name: "FW" }
]
function App() {
return (
<div className="App">
<h1>Mapping object keys in react and returning child properties
</h1>
{Object.entries(arrayvals).map((arr)=>{
return <div>Number is : {arr[1].Number} || NewNumber is : {arr[1].newNumber} || and Value is : {arr[1].name}</div>
})}
</div>
);
}
How to get value without knowing key in map function of react js
}) } )) ... You can get the keys from an object using Object.keys(object). After that you can loop through the keys and access each key on each object. class ResponseTable extends React... More on stackoverflow.com
Problem with Object.keys.map in React
How to find the keys of an element which we rendered dynamically with map in react js?
reactjs - Retrieve element's key in React - Stack Overflow
you can only map through an array. You are mapping through stream.results which appears to be an object. You need to use another option to loop through the objects properties to get to the values. There is a few ways you can achieve this and what is going to be best will be dependant on your scenario and exactly how you want to display the data.
for in loop, Object.keys(yourobject), Object.entries(yourobject) and Object.values(yourobject) are all options for getting into objects and extracting specific data. You need one or a combination of those to get your information.Play around with those in the console and read up about them on MDN If you get to an actual array, you can map that.. ex rent and buy can be mapped.
You are trying to map an object while the function is intended for Arrays. To get around that, create an array of your object keys and map over them.
return (
<div>
{Object.keys(results).map(key => (
<li>{results[key].link}</li>
)}
</div>
)
Object.entries will return all the properties and values as keys and values in array,
Object.keys will return all the properties in array and
Object.values will return all the values in array
this.state.response.map((data, i) => (
<tr key={i}>
<td>{i+1}</td>
{
console.log(Object.entries(data));
Object.values(data).map(d => <td>{d}</td>);
}
<td></td>
</tr>
Use method Object.keys() to get an array of keys in the given object
{
this.state.response !== "" &&
this.state.response.map((data, i) => (
<tr key={i}>
<td>{i+1}</td>
{
Object.keys(data) //Returns array of all keys: Array ["number", "name", "type", "contact"]
}
<td></td>
</tr>
))
}
The best way to get the key attribute value that you set is to just pass it as another attribute as well that has meaning. For example, I often do this:
const userListItems = users.map(user => {
return <UserListItem
key={ user.id }
id={ user.id }
name={ user.name }
});
or in your case:
this.props.albums.map(function(albumDetails) {
return (<div className="artistDetail" key={albumDetails.id} data-id={ albumDetails.id } onClick={component.getTracks}>
<img src={albumDetails.imageSrc} />
<p><a href="#" >{albumDetails.name}</a></p>
</div>)
It seems redundant, but I think its more explicit because key is a purely react implementation concept, which could mean something different than id, even though I almost always use unique IDs as my key values. If you want to have id when you reference the object, just pass it in.
The key is for React's internal use and won't display in the HTML and Console, you just have to use the id to retrieve that unique component. eg:
getTracks: function (e) {
console.log(e.target.id?e.target.id:e.target);
}
this.props.albums.map(function(albumDetails) {
return (<div key id={ albumDetails.id } onClick={component.getTracks} className="artistDetail" >
<img src={albumDetails.imageSrc} />
<p><a href="#" >{albumDetails.name}</a></p>
</div>)
}
And also you don't need to pass value to the key attribute.Value is completely optional.
React keeps telling me I have a unique key issue with the following code. I have keys on all my mapped items, v4() returns a key in all my console logs, I also have tried 5 or 6 different ways to set unique keys. The error is actuallly telling me Its on the h1 which has no need for a key, if i delete the h1 it just tells me the line above it is the issue and continues that pattern. I have never had an issue with this. The only way to get rid of the error is to remove the map alltogether, removing either one of the list items and the ternary temporarily has no effect on the error.
<h1>ChatBox</h1>
</header>
<main className='chatBoxBody'>
{chatMessages.map((m, i) => (
<>
{m.id ? (
<h3 key={v4()}>{`${m.user} has joined `}</h3>
) : (
<h3
key={v4()}
className={user.user === m.user ? 'user' : 'friend'}
>{`${m.user}: ${m.message}`}</h3>
)}
</>
))}
</main>side note- it only gives me the key error once at loadtime, never again on repeated messages.
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.