I have an automated generated object
this.state={
fruits = []
}
Here is how I generate objects and insert to fruits
const gen = (fruitname, price) => {
this.setState({
this.state.fruits: {...this.state.fruits, [fruitname]: price}
})
}
output is
{apple: "$2", banana: "$4", orange: "$6"}
Now I need to nest types in looks like below
{apple: {
"red": "$3",
"Cripps": "$3",
"Honeycrisp": "$5"
},
banana: {
"small": "$4",
"yellow": "$5",
"green": "$2"
},
...
}
I updated code to
const gen = (fruitname, price, types, eachPrice) => {
this.setState({
this.state.fruits: {...this.state.fruits, [fruitname]: { [types]: eachPrice} }
})
}
However, I only get one object of each fruit that each time I type
{apple: { "red": "$3", }, banana: { "small": "$4", }, ... }
How to keep all ?
typesbe an array like["red", "Cripps", "Honeycrisp"]and theneachPricewould also be an array which has the prices like["$3", "$3", "$5"]?