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

Convery JS Object to array – keep key & value

I am trying to convert an object (updatedConfig) to an array (configArray), while maintaining the same structure (and avoid further nesting).

I have tried declaring a new array const and using Object.entries to push the keys & values.
I am able to get the keys but am having trouble figuring out how to achieve the nesting of array.

const configArray = [];
    
    Object.entries(updatedConfig).forEach(([key, value]) => {
        configArray.push(key);     
    })

Here is the object in question:
enter image description here

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 try something like this using Object.entries and Array.map

const configObject = {
 key1: 'value',
 key2: 1,
 key3: [1, 2, 3]

}

const configArray = Object.entries(configObject).map(([key, value]) => ({key, value}))

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