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

99
Views
How can we convert below arrays into objects using reduce and rest operator

Can any please help me for converting following arrays into object:

let data = [
  { name: "A", job: "soft", address: "ggn" },
  { name: "B", job: "software", address: "GGN" },
  { name: "C", job: "software", address: "GGN" },
];
Output:  {
“A”:{job: “software”, address: “GGN”},
“B”:{job: “software”, address: “GGN”},
“C”:{job: “software”, address: “GGN”}
};

Here I don't want name and its value of objects.

I tried using the following solution:

const output = data.reduce((object, x) => {
    return { ...object, [x.name]: x };
}, {});

But I am getting different answer. Can somebody help by giving optimum solution. We can solve it using reduce and rest operator.

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

0

You can use it like this:

let data = [
    { name: "A", job: "soft", address: "ggn" },
    { name: "B", job: "software", address: "GGN" },
    { name: "C", job: "software", address: "GGN" },
];

const output = data.reduce((acc, curr) => {
    const { name, ...rest } = curr;
    acc[name] = rest
    return acc;
}, {});

OR:

const output = data.reduce((acc, curr) => {
    const { name, ...rest } = curr;
    return { ...acc, [name]: rest };
}, {});
about 4 years ago · Juan Pablo Isaza Report

0

You can do like this

let data = [{
    name: "A",
    job: "soft",
    address: "ggn"
  },
  {
    name: "B",
    job: "software",
    address: "GGN"
  },
  {
    name: "C",
    job: "software",
    address: "GGN"
  },
];


const output = data.reduce((object, x) => {
  const {
    name,
    job,
    address
  } = x;
  object[name] = {
    job,
    address
  }

  return object;
}, {});
console.log(output)

about 4 years ago · Juan Pablo Isaza Report

0

let data = [
    {name: 'A', job: 'soft', address: 'ggn'},
    {name: 'B', job: 'software', address: 'GGN'},
    {name: 'C', job: 'software', address: 'GGN'},
];

const output = {};

const obj = data.reduce((acc, el) => {
    const { name, job, address } = el;
    return { ...acc, [name]: { job, address} };
}, output);

console.log(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!