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

120
Views
On submit send data to API by taking data from an array of objects in react

On submit I want to call a function and send data as object of key value , taking data from an array of objects


const [documents,setDocuments] = useState({}); 

let selected= [
          {name: 'Profile', value:'S'}, 
          {name: 'Pan card', value:'P'},           
          {name: 'Document(Investment proof) ', value:'IP'}
         ]


function onSubmit(){

  selected.map(data=>setDocuments({...documents, [data.value]: data.value})) 
  axios.post(`api`, documents)

}

result should be like this: axios.post(api, {S:'S', P:'P', IP:'IP'})

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

0

useState and setState both are asynchronous. if you send object use reduce.

const obj = selected.reduce((pre,curr)=>{
   pre[curr.value] = curr.value
   return pre
 },{}) 

axios.post(`api`, obj)

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!