Reputation: 193
const checked = [{
food:['apple', 'banana']
drink:['wine', 'beer']
}];
render (
<>
{checked.map((value) => {
value.food.forEach((each) => (
<div>{each}</div>
)
)}
</>
)
I tried this way and noting is shown in browser.. what would be the best way to approach?
Upvotes: 0
Views: 590
Reputation: 41
Can check this approach. if you want to print just food values, below code should work. If you want to print all the values (both food and drink), then uncomment the commented code below.
export default function App() {
const checked = [
{
food: ["apple", "banana"],
drink: ["wine", "beer"]
},
{
food: ["grapes", "oranges"],
drink: ["coke", "sprite"]
}
];
// to display values of all arrays.
// return (
// <>
// {checked.map((value) => {
// const keys = Object.keys(value);
// return keys.map((eachKey) => {
// return value[eachKey].map((individualValue) =>
// (
// <div>{individualValue}</div>
// )
// )
// });
// })
// }
// </>
// );
// To display just food values.
return (
<>
{checked.map((value) => {
return value.food.map((each) => <div>{each}</div>);
})}
</>
);
}
Upvotes: 0
Reputation: 16374
You are iterating over the checked
array items using forEach
which won't induce any results since the forEach
method
executes a provided function once for each array element.
which won't result in a transformed array.
What you are looking for is the map
method which
creates a new array populated with the results of calling a provided function on every element in the calling array.
hence returning your transformed items so that they can be rendered (transformed at compilation time to ReactElement
using the JSX syntax).
Note that you need to use an HTML tag instead of a React.Fragment
the empty tag <>
syntax:
const checked = [{
food:['apple', 'banana'], // there is a missing comma here
drink:['wine', 'beer']
}];
render ( // render and not rander
<div> // div instead of empty tag
{checked.map((item) => item.food.map((each) => <div>{each}</div>))}
</div>
)
Upvotes: 1
Reputation: 591
There is a couple of improvements that require to be implemented to make the list displayed.
First, the map method does not return anything. Two solutions:
checked.map((value) => value...
checked.map((value) => { return value...}
The other issue is that the second loop is iterated using the forEach
method.
The difference between the two (forEach
and map
) from MDN:
The forEach() method executes a provided function once for each array element. MDN
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array. MDN
Basically, it means that forEach does not return anything and that why you need to use map
checked.map((value) => {
return value.food.map((each) => (<div>{each}</div>))
})}
or
checked.map((value) =>
value.food.map((each) => (<div>{each}</div>))
)}
Upvotes: 1
Reputation: 721
Your code has multiple errors.
render
instead of randerforEach
doesn't return any thing. It just iterates over an array. So, if you want to return something (in this case a div
element), use map
.key
for each div element otherwise react would give you a warning in the console. This is done so that while re-rendering, based on the keys, react would understand which component to re-render and which to skip. Otherwise all the div would be re-rendered which is a costly operation.const checked = [
{
food: ["apple", "banana"],
drink: ["wine", "beer"]
}
]
return (
<>
{checked.map((value) => {
return value.food.map((each, index) => {
return <div key={index}>{each}</div>;
});
})}
</>
);
Upvotes: 1
Reputation: 27
Need to Return Your data like below!!
import React from "react";
export default function App() {
let checked = [{
food:['apple', 'banana'],
drink:['wine', 'beer']
}];
return (
<div className="App">
{
checked.map((item) => {
return item.food.map((fruit)=>{
return <h1>{fruit}</h1>
})
})
}
</div>
); }
Upvotes: 2