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

How to insert Objects in object into variable

I have an objects inside parent object. Can I create an object in which there are these objects?

let a = {
  "11111": {"1": "test", "11": "test"},
  "22222": {"2": "test", "22": "test"}
} 

Expected output:

{
  "1": "test", 
  "11": "test",
  "2": "test", 
  "22": "test",
}

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

>Solution :

Just reduce the object values by spreading them onto a new object.

const
  a = {
    "11111": { "1": "test", "11": "test" },
    "22222": { "2": "test", "22": "test" }
  },
  b = Object.values(a).reduce((acc, obj) => ({ ...acc, ...obj }), {});
  
console.log(b);

If you want to be efficient, you can call Object.assign instead:

const
  a = {
    "11111": { "1": "test", "11": "test" },
    "22222": { "2": "test", "22": "test" }
  },
  b = Object.values(a).reduce((acc, obj) => Object.assign(acc, obj), {});
  
console.log(b);
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