I have a string variable in javascript called temp:
temp = "[{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}]"
How I can convert the string temp to array?
To be like:
temp = [{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}]
I have a string variable in javascript called temp:
temp = "[{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}]"
How I can convert the string temp to array?
To be like:
temp = [{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}]
If the string were in valid JSON format you could use JSON.parse()
. But it's not, so the only general solution (short of writing your own parser) is eval()
.
var temp = "[{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}]"
var result = eval(temp);
console.log(result);
This can be extracted as array
using eval javascript function.
const input = "[{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}]";
const output = eval(input);
console.log(output);
const arr = ['foo,', 'bar', 'red,', 'car']
console.log(arr.join(' '))//"foo, bar red, car"
You can simply use the join() function to convert the array into string
JSON.parse
should work, if I understand the question correctly
You can use the answer from this question for fixing the JSON before parsing it