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 a sentence to an object with Key/Value Format js

I have a sentence like below:

mySentence = "12,alex \n" +"22,zac \n" +"41,sara \n" +"33,mike \n"

and want to convert into an object like below:

{
"12":"alex",
"22":"zac",
"41":"sara",
"33":"mike"
}

any solution would be my appreciated

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 :

Try this

var mySentence = "12,alex \n" +"22,zac \n" +"41,sara \n" +"33,mike \n"
mySentence = Object.fromEntries(mySentence.trim().split(' \n').map(v => v.split(',')))
console.log(mySentence)
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