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

Is there a way to convert a string to a proper array, not an array of strings or an array of characters?

I have this string “[{value: 1400, value1: 50, value2: 48}, {value: 1500, value1: 20, value2: 88}]" and I need to convert into a list [{value: 1400, value1: 50, value2: 48}, {value: 1500, value1: 20, value2: 88}] in JavaScript, is there a way to do that??

>Solution :

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

you can use the eval() function. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#never_use_eval!)

Please be aware, that eval() shouldnt be used generally as you can read in the article above.

Example:

const array = eval('[{value: 1400, value1: 50, value2: 48}, {value: 1500, value1: 20, value2: 88}]');

Edit:
I would suggest to modify you string to fit the JSON-Standard.
To achieve this, you would have to put the keys of your objects in quotation marks like this:

let yourString =  '[{"value": 1400, "value1": 50, "value2": 48}, {"value": 1500, "value1": 20, "value2": 88}]';
const array = JSON.parse(`${yourString}`);

This will work ^^

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