Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Generate objects with dynamic "key" using spread operators

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use it like this:-

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!