0

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 ?

3
  • what determines the price for each type? Your function only accepts one price, but your output has multiple different prices for each type Commented Feb 15, 2019 at 2:10
  • so would types be an array like ["red", "Cripps", "Honeycrisp"] and then eachPrice would also be an array which has the prices like ["$3", "$3", "$5"]? Commented Feb 15, 2019 at 2:13
  • 1
    @NickParsons yes! Thx Commented Feb 15, 2019 at 2:25

2 Answers 2

1

If I understand correctly, you are trying to map an array of types (keys) to values (eachPrice) in an object. If this is the case you can use .reduce to achieve this. Here I am reducing the types array to an object, where each type is a key and each value is its corresponding price from the eachPrice array.

See example below:

let state = {}
const gen = (fruitname, types, eachPrice) => {
  state.fruit = {...state.fruit,
    [fruitname]: 
      types.reduce((acc, t, i) => (
        { ...acc, [t]: eachPrice[i]}
      ), {})
  }
}

gen("apple", ["red", "Cripps", "Honeycrisp"], ["$3", "$3", "$5"]);
gen("banana", ["small", "yellow", "green"], ["$4", "$5", "$2"]);
       
console.log(state.fruit);

Note that this function doesn't use the price argument as you have eachPrice to define the prices.

Sign up to request clarification or add additional context in comments.

Comments

1

First clone the state.

check if it is already in state, if it's there than just adjust that particular key/value and manipulate if not than create a new one.

let state = {apple:{"red":"$3","Cripps":"$3","Honeycrisp":"$5"},banana:{"small":"$4","yellow":"$5","green":"$2"},}

function handleState(name,type,price){
  if(state[name]){
    let temp = {...state}
    temp[name][type] = price
    return {
      ...temp,
    }
  } else {
    return{
      ...state,
      [name]:{
        [type]: price
      }
    }
  }
}

console.log(handleState('apple','red','$20'))
console.log(handleState('test','red','$20'))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.