Reputation: 470
I have this data in an input: [16,57.35], [23,56.26], [34,54.57]
and I want to turn it into an array
var data =$('#data').val();
var array = JSON.parse ("["+data+"]");
I'm having this error
Uncaught SyntaxError: Unexpected token.
How I can fix it or I can convert the input value in array?
Upvotes: 0
Views: 5174
Reputation: 148180
Your code is working check it here, you may need to include required jQuery library or check some thing else in the code causing it.
data = $('#txt1').val();
arr = JSON.parse ("["+data+"]");
console.log(arr);
Upvotes: 4
Reputation: 46077
Try using the eval
function:
var data = "123, 456, 789";
var array = eval("[" + data + "]");
You'll need to make sure that whatever you're inputting is valid JSON, but the above code will output an array for you. Hope it helps.
Upvotes: 1