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

Remove single quotes from Javascript object keys

I’m trying to have the key of the object not be surrounded by single quotes.

let arr= [2, 3, 4, 11];
let myObj={};
myObj[arr[2]]=true;
console.log(myObj);

The above code outputs the result { ‘4’: true }

How do I remove the single quotes around 4 and have it display as { 4: true }

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 do it like so:

const arr = [2, 3, 4, 11];
const myObj = {};
myObj[arr[2]] = true;
const str = JSON.stringify(myObj);
console.log(str.replace(/[",']/g, ''));
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