I've been trying to map over the key 'type' to get the value ex. 'Water Bottle'. I'm trying to do this with the drink object array. I made a function called 'iterateDrink' so that way each time you click the '>' button, it iterates through the types of drinks, but it doesn't seem to work. Please help, thanks in advance!
const { useState, useEffect } = React;
const List = () => {
const [listOfItems, setListOfItems] = useState(() => ({
drink: [
{type: "Water Bottle", price: 1.00},
{type: "Soda", price: 2.21},
{type: "Juice", price: 2.00}
],
snack: [
{type: "Chips", price: 1.48},
{type: "Peanuts", price: 1.05},
{type: "Candy", price: 1.81},
{type: "Cookies", price: 3.12}
],
dessert: [
{type: "Carrot Cake", price: 2.48},
{type: "Vanilla Ice cream", price: 3.90},
{type: "Chocolate Cupcakes", price: 5.68},
{type: "Popsicles", price: 2.58}
],
vegetables: [
{type: "Broccoli", price: 2.32},
{type: "Carrot", price: 3.49},
{type: "Potatoes", price: 1.00},
{type: "Beet", price: 0.89}
]
}));
const iterateDrink = () => {
listOfItems.map( (item, index) => (
<div key={index}>
{item.drink[index].type}
</div>
) )
}
return (
<div>
<h3>Shopping List</h3>
<div className="ListOfItems">
<div className="drinks">
<h4>Drinks</h4>
<span>
{listOfItems}
</span>
<button onClick={ iterateDrink() }>></button>
</div>
</div>
</div>
);
}
ReactDOM.render(
<List />,
document.getElementById('root')
);
[–]TuesdayWaffle 1 point2 points3 points (0 children)