-1

How to convert string

"{7: {type: 'line'},8: {type: 'line'}}" 

into JavasScript OBJECT

{7: {type: 'line'},8: {type: 'line'}}
Axel Amthor
  • 10,980
  • 1
  • 25
  • 44
  • Yes, I saw this posts but it didn't work for me. I'm trying to use google ComboChart and they use a option like this: `series: {4: {type: "line"},5: {type: "line"}}` I build the string `4: {type: "line"},5: {type: "line"}` from my program but I cannot convert it into a valid 'option' value for googlecombochart: https://google-developers.appspot.com/chart/interactive/docs/gallery/combochart – user3217054 Feb 13 '14 at 22:05

1 Answers1

1

Use JSON.parse

JSON.parse(STRING) // convert to object
JSON.stringify(STRING) // convert object to string, that can be used in JSON.parse

Don't use numbers as keys, because it bad practice you can't access objects like this:

var a = {"8": "value"};
// Invalid syntax
console.log(a.8)
// work
console.log(a["8"], a[8]);

var b = {"eight" : "value"};
// work
console.log(b.eight);
Farkhat Mikhalko
  • 3,565
  • 3
  • 23
  • 37