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.

>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);

Leave a Reply