1

I want to generate an object where the key is the value of the index from the mapped data as shown in the code below

{data.map(({ quest, answers }, index) => {
                  <Radio.Group
                    name={index}
                    defaultValue={def}
                    onChange={(value) => {
                    setVal({...(val), index: JSON.stringify(value)}  )
                      
                    }}
                  >
                    {answers.map((ans, key) => {
                      return (
                        <Radio value={key + 1}>
                          {ans}
                        </Radio>
                      );
                    })}
                  </Radio.Group>
})}

So "index" in this line

setVal({...(val), index: JSON.stringify(value)} )

should be from this line

{data.map(({ quest, answers }, index) => {

The problem is the code above returns the index value as the string "index" instead of the value of index from the mapped data i am working with

1
  • 1
    Well makes sense, maybe show us how the value object looks like? Commented Jan 2, 2022 at 6:45

1 Answer 1

4

Use it like this:-

setVal({...(val), [index]: JSON.stringify(value)} )

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

1 Comment

Exactly what I needed

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.