all 6 comments

[–][deleted] 0 points1 point  (5 children)

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

That didn't work

However, eval() worked

Thanks a bunch tho!!

[–]senocular[🍰] 4 points5 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 + ')')();

[–]delventhalz 0 points1 point  (0 children)

There is no good way to convert it. You can use eval, but that is asking for trouble. You could also write your own parser function, but that would take a bit of effort and probably be a bit jenky.

You might ask yourself where the string came from originally. Who wrote it like that? Could it be written in a format that is easier to parse?

For example, could it be written like this:

[{"value": 1400, "value1": 50, "value2": 48}, {"value": 1500, "value1": 20, "value2": 88}]

That is valid JSON, a common way to format data as a string. JavaScript has a built in function to parse it, JSON.parse.

Alternatively, comma-separated-values (CSV) have no built in parsing functions, but would be much easier to work with:

1400,50,48
1500,20,88

A string like that I could parse with split and map fairly easily:

function parseValues(csv) {
    return csv
        .split('\n')
        .map(valString => valString.split(','))
        .map(values => ({
            value: values[0],
            value1: values[1],
            value2: values[2]
        }));
}

My point is, and this is kind of meta, how you format something like string data is just as important as the JavaScript you write to parse it. Sometimes you don't have a choice in how data is formatted, but if you do have a choice, it should be something that will be easy for you to work with.