Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert an array of object with different keys to a single object

I have an array of object like this

[
 0:{new: 'val'},
 1:{new2: 'val2'},
 2:{new3: 'val3'}
]

I’m trying to convert it to this form

{new:'val',new2:'val2',new3:'val3'}

I tried the related answers for similar questions and tried using

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var arr = [{key:"11", value:"1100"},{key:"22", value:"2200"}];
var object = arr.reduce(
  (obj, item) => Object.assign(obj, { [item.key]: item.value }), {});

console.log(object)

but that return {undefined,undefined}

>Solution :

var arr = [{
    new: 'val'
  },
  {
    new2: 'val2'
  },
  {
    new3: 'val3'
  }
];
var object = arr.reduce(
  (obj, item) => Object.assign(obj, item), {});

console.log(object)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading