0

Im working with the datatables plugin with JQUERY and I have to get my string into an array. The below code works when I manually set the array.

var myarray = [['Joe','Smith',20,3], ['Kayla','Smith',32,1]]
dt.clear().rows.add(myArray).draw();

when I run console.log(myarray)

[Array[4], Array[4]]

now when I have a string that I get from the back end its in the exact same format, however, like i said its a string.

I tried something like this on a string thats in this format: [['Joe','Smith',20,3], ['Kayla','Smith',32,1]]

var myArray = new Array(mystring);

but when I run console.log(myArray) I get

["[['Joe','Smith',20,3], ['PG','Kayla','Smith',32,1]]"]

Not sure how to get it in the right format. Thanks.

0

1 Answer 1

1

You cannot simply turn a string representation of an array into an array by passing it to the Array constructor. You will need to use valid JSON syntax (i.e. replacing single ' quotes with " double ones).

You can then use JSON.parse() to parse the string back into an array as follows:

let myArray = JSON.parse('[["Joe","Smith",20,3],["Kayla","Smith",32,1]]');
console.log(myArray);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.