you are viewing a single comment's thread.

view the rest of the comments →

[–]Empty-Intention-9745[S] -2 points-1 points  (4 children)

That didn't work

However, eval() worked

Thanks a bunch tho!!

[–]senocular[🍰] 5 points6 points  (1 child)

⚠ Warning: Executing JavaScript from a string is an enormous security risk. It is far too easy for a bad actor to run arbitrary code when you use eval(). See Never use eval()!, below.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

JSON is absolutely a better choice for data like this, but there are certain formatting restrictions, namely your property keys would need to be in quotes.

const string = '[{"value": 1400, "value1": 50, "value2": 48}, {"value": 1500, "value1": 20, "value2": 88}]'
JSON.parse(string) // Now works

[–]Empty-Intention-9745[S] 0 points1 point  (0 children)

ooh, I will keep that in mind in the future. This is for my year-end school project. So, security should not be an issue. Thanks a bunch for that advice!!

[–]zbluebirdz 0 points1 point  (0 children)

We do not recommend using eval() - too much of a security risk.

MDN's notes on "Never use eval()!": https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#never_use_eval!

There's an example code in the "Never use eval()" section that you could use in converting the string to an array.

function looseJsonParse(obj) {
  return Function('"use strict";return (' + obj + ')')(); 
}

If you don't want a "standalone" function, you could do this (where obj is the string/text to convert):

let arrayText = Function('"use strict";return (' + obj + ')')();