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

convert array to array of objects using JS

I need a help, how to convert an array to array of objects.

Eg: may be this is the array

let a = ["abc", "mno"]

I want it to be like this:

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

let b = [
  {
    "text": "abc"
  },
  {
    "text": "mno"
  }
]

please help me to fix this. Thanks.

>Solution :

const a = [ "abc", "mno" ];
const b = a.map( ( item ) => { return {text:item} } );

console.log('a => ', a)
console.log('b => ', b)

explanation :

array.map is looping to every element of the array and if you provide return it will create new array from the return of every element

in this case the return is an object of { text:item } by item being the element of which index on the loop

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