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

151
Views
how can i change array of object to object of array in javascript?

I am trying to convert array of objects to object of array using javascript.

Here is the code

const data = [
{
  items: [
    {
      code: "location_list_page",
      icon: "environment",
      id: 1,
      link: "/configurations/locations",
      name: "Localisations",
    }
  ]
},
{
  items: [
    {
      code: "service_list_page",
      icon: "api",
      id: 5,
      link: "/configurations/services",
      name: "Services",
    }
  ]
 }
]

I want to create this object:

const data = {
  items: [
    {
      code: "location_list_page",
      icon: "environment",
      id: 1,
      link: "/configurations/locations",
      name: "Localisations",
    },
    {
      code: "service_list_page",
      icon: "api",
      id: 5,
      link: "/configurations/services",
      name: "Services",
    }
  ]
}

I try to solve it using map in array method but it didn't work.

Anyone can help me please ???

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

0

This is a pretty straightforward use of flatMap:

const data = [
{
  items: [
    {
      code: "location_list_page",
      icon: "environment",
      id: 1,
      link: "/configurations/locations",
      name: "Localisations",
    }
  ]
},
{
  items: [
    {
      code: "service_list_page",
      icon: "api",
      id: 5,
      link: "/configurations/services",
      name: "Services",
    }
  ]
 }
]

const result = {items: data.flatMap(x => x.items)};

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

it can be solved using map function and Object.assign() and the spread operator

var new_data = {"items": data.map(x=> Object.assign({}, ...x["items"]))};
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!