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 convert below array data to object with property (json) in javascript or es6?

My array

const array = [2,3];

and I want object like

"media" :[{"mid":"2"},{"mid":"3"}]

Thanks in advance.

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 use Array.prototype.map to create the desired array and then you can create an object and assign the array to the media key.

const array = [2, 3];
const obj = { media: array.map((item) => ({ mid: item })) };

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