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

Change array of elements to objects with static key value

So, I have an array as follows.

Array.from(Array(3).keys())
//=> [0, 1, 2]

I wanted to convert this array to object and assign a static key called value to each elements.

So the final result should be

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

result = [
{
value: 0,
},
 {
value: 1,
},
{
value: 2,
}
]

>Solution :

Just map it:

const source = Array.from(Array(3).keys());
const result = source.map(x => ({value: x}));

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