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

150
Views
Formatting Arrays of Objects into Objects (ES6 Way)

I am trying to create a pretty Objects of arrays but this come instead.

{
    "label": [
        "Instagram"
    ],
    "value": [
        "@username"
    ]
}

So, how can I change it? What I want is to be like this

{
    "label": "instagram",
    "value": "@username"
},

I don't know how it happened, but my guess is it was the result of me using formik to define initialValues of a complex nested array from strapi. I was using array.map to map the objects. Hence perhaps thats why it was so messy.

So what is the solution for this? Formatting Arrays of Arrays into Objects? Merging? Converting? I have no idea what it was called. Thanks in advance for anybody replying this.

(updated) The initialValues:

const formik = useFormik({
enableReinitialize: true,
initialValues: {
  name: vendor?.name || '',
  description: vendor?.description || '',
  company: {
    social_media: [
      {
        label: vendor.company?.social_media.map((x) => x.label) || '',
        value: vendor.company?.social_media.map((x) => x.value) || ''
      }
    ]
  }
},
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Array.map returns an array if you don't want any object use Array.foreach

Or

Use [...Array.map()]

    label: [...vendor.company?.social_media.map((x) => x.label)] 
    value: [...vendor.company?.social_media.map((x) => x.value) 
about 4 years ago · Juan Pablo Isaza Report

0

You can use for..in and array join method

const data = {
  "label": [
    "Instagram"
  ],
  "value": [
    "@username"
  ]
};

for (let keys in data) {
  data[keys] = data[keys].join(',')

};

console.log(data)

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!