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

Create an object based on another object

I have an object that looks like this:

let Object1 = { id1: { date: '23-01-2023', status: 'completed' }, id2: { date: '02-01-2023', status: 'pending' } }

How can I make a new object that has only every key of the object, but also the value of one of the keys on the nested object, like this:

let Object2 = { id1: 'completed', id2: 'pending' }

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 :

You can map over Object.entries, replacing each value with the status property of that nested object, then create a new object with Object.fromEntries.

let obj1 = { id1: { date: '23-01-2023', status: 'completed' }, id2: { date: '02-01-2023', status: 'pending' } }
let res = Object.fromEntries(Object.entries(obj1).map(([k, v]) => [k, v.status]));
console.log(res);
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