I have to map through an object and display all the values from all of the object keys in a list. The object looks like this.
const books = {
book1: ['comedy', 'action', 'romance'];
book2: ['horror', 'advanture', 'crime'];
book3: ['fantasy, 'humour', 'war'];
}
I tried to create a separate array with all the keys using Object.values
like this.
const objValues = Object.values)books
but the result was like this
Array[2]
array[0]
['comedy', 'action', 'romance']
Array[2]
array[1]
['horror', 'adventure', 'crime']
so on...
I have also tried to map through this objValues
array and add each value to a new array that I could map over in JSX using forEach
but that didn't work.
const allValues = () => {
const res: any = [];
objValues.forEach(key: any) => {
key.forEach((value: any) => {
res.push(value);
});
});
return res;
};
How could I map through all of those values and display them in a list? Maybe this helps.
this is how I wanted to look
{object.map((value) => {
<div>
<span>{value}</span>
</div>
})}
comedy
action
romance
horror
adventure
crime