Home > Software design >  How can we convert below arrays into objects using reduce and rest operator
How can we convert below arrays into objects using reduce and rest operator

Time:11-12

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.

CodePudding user response:

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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can use .map() and .reduce() in combination with Object.assign() as follows:

let output = data.map(({name,...rest}) => ({[name]:rest}))
.reduce((acc,cur) => Object.assign(acc,cur),{});

DEMO

Show code snippet

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

let output = data.map(({name,...rest}) => ({[name]:rest}))
    .reduce((acc,cur) => Object.assign(acc,cur),{});

console.log( output );
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Alternatively, you can use .map() and .reduce() as follows:

let output = data.map(({name,...rest}) => ({[name]:rest}))
.reduce((acc,cur) => ({...acc,...cur}),{});

DEMO

Show code snippet

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

let output = data.map(({name,...rest}) => ({[name]:rest}))
    .reduce((acc,cur) => ({...acc,...cur}),{});

console.log( output );
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Or, if your objects do not have too many properties:

let output = data.reduce((acc,cur) => ({...acc,...{[cur.name]: {job:cur.job,address:cur.address}}}),{});

DEMO

Show code snippet

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

let output = data.reduce((acc,cur) => ({...acc,...{[cur.name]: {job:cur.job,address:cur.address}}}),{});

console.log( output );
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

No reduce needed here.

Object.fromEntries() to the rescue - you can use .map and destructuring to map the objects into an array of pairs, which fromEntries can turn into a plain object:

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

> Object.fromEntries(data.map(({name, ...rest}) => [name, rest]));
{
  A: {job: 'soft', address: 'ggn'}
  B: {job: 'software', address: 'GGN'},
  C: {job: 'software', address: 'GGN'}
}

CodePudding user response:

The easiest way is to de-structure the objects first, so you can separate the name. Then you can just return them with the accumulator.

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

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

console.log(output)
<iframe name="sif6" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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 };
}, {});

CodePudding user response:

first you need to change the double quotes, they are not the standard double quotes you can write it in much shorter code with map method

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


const toto = data.map ( element => { 

const name = element.name 
delete element.name 
return {[name] : element}})

console.log(toto)
<iframe name="sif7" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

check map method to learn more : https://learnjsx.com/category/2/posts/es6-mapFunction

  • Related