Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  174] [ 3]  / answers: 1 / hits: 18240  / 11 Years ago, thu, october 17, 2013, 12:00:00

I have an ajax request that returns a list of values like this:



[-5, 5, 5], [-6, 15, 15], [7, 13, 12]


I need it to be a javascript array with numbers:



[[-5, 5, 5], [-6, 15, 15], [7, 13, 12]]


I tried to replace the '[' and ']' for a '|' and then split by '|' and foreach item split by ',' and add them to an array, but this is not elegant at all.



Do you guys have any suggestions?


More From » arrays

 Answers
4

You can use JSON.parse() to convert that string into an array, as long as you wrap it in some brackets manually first:



var value = [-5, 5, 5], [-6, 15, 15], [7, 13, 12];
var json = JSON.parse([ + value + ]);

console.log(json);


I would suggest correcting the output at the server if possible, though.


[#74934] Wednesday, October 16, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
magaly

Total Points: 524
Total Questions: 96
Total Answers: 89

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
;