Like this:
{attributes.map((items, index) => {
return (
<ul key={index}>
{Object.keys(items).map((key) => {
return (
<li key={key + index}>{key}:{items[key]}</li>
)
})}
</ul>
)
})}
Answer from CD.. on Stack OverflowREACT: Render the keys of only one object
REACT: Render the keys of only one object
reactjs - How to display object's keys and values in react component - Stack Overflow
reactjs - how to map an Object.keys array inside an Object.keys in react.js? - Stack Overflow
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.
» npm install react-key-from-object
Are you asking how to access properties with problematic names like -LMgzJGM78f0BHbPf8cc?
If so, instead of the object.property notation, you can access object properties by the property name using the square brackets syntax:
let obj = { color: 'blue' }
let prop = 'color'
console.log(obj.color);
console.log(obj['color']);
console.log(obj[prop]);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
If not, please try to make more clear what your current problem is.
I'd suggest to transform the object received from the Firebase to array in this way:
const formattedTasks = [];
const tasks = Object.values(data.tasks);
tasks.forEach(task =>
Object.entries(task).forEach(([key, value]) =>
formattedTasks.push({ name: key, data: value })
)
);
So, you'll map through formattedTasks array.
Here's a working example: https://codesandbox.io/s/l348nnkv9q
I had a similar situation where the object has no unique id.
I ended up generating ids for the items based on object references:
let curId = 1;
const ids = new WeakMap();
function getObjectId(object) {
if (ids.has(object)) {
return ids.get(object);
} else {
const id = String(curId++);
ids.set(object, id);
return id;
}
}
// Usage
<RuleList rule={rule} key={getObjectId(rule)} />
I know you mentioned that you don't want to generate ids, but I thought I'd share this since it is generic and doesn't depend on any properties in your object.
I created a lib intended to solve this problem. It's based on the idea suggested by @amann (using a weakMap). It provides a hook, so that the weakMap is created and destroyed with the component.
Have a look to https://www.npmjs.com/package/react-key-from-object
import { useKeyGen } from 'react-key-from-object'
const DogList = () => {
const keyGen = useKeyGen();
return (
<ul>
{dogs.map((dog) => (
<li key={keyGen.getKey(dog)}>
{dog.name}
-
{dog.age}
</li>
))
</ul>
);
}