I have this state Object in a class:
state = {
ingredients: {
salad: 1,
bacon: 5,
cheese: 2,
meat: 2
}
}
and want to convert it into an array that gets also the value number, like this:
const burger = (props) => {
const transformedIngredients = Object.keys(props.ingredients)
.map(igKey => {
return [...Array(props.ingredients[igKey])].map((_,i)=>{
return <BurgerIngredient key={igKey + i} type={igKey}/>
});
});
console.log(transformedIngredients)
return(
<div className={classes.Burger}>
<BurgerIngredient type="bread-top" />
<BurgerIngredient type="cheese" />
<BurgerIngredient type="meat" />
<BurgerIngredient type="bread-bottom" />
</div>
);
};
export default burger;
I succeeded in doing it following a tutorial and to be honest I do not completely understand what is happening here. especially this part:
return [...Array(props.ingredients[igKey])].map((_,i)=>{
return <BurgerIngredient key={igKey + i} type={igKey}/>
});
any help would be greatly appreciated! Thank you!