Karim
Karim

Reputation: 15

Split a multidimensional javascript array

I have an array that looks something like this

[[1,2,3],[1,2,3]]

Because of the way it is being received (via ajax) it is being read as a string instead of an array using .split(',') doesn't work here.

console.log shows it as [[1,2,3],[1,2,3]] so I know the data is going through, however if I were to put the array directly in the page it shows properly as array array

This is the ajax with the recommendation given below. It still comes as plaintext.

$.ajax({
    url: "file.php" + "?param=" + param,
    type: "POST",
    data: 'data',
    datatype: 'json',
    success: function (data) {
        object = JSON.parse(data);
        filters();
    }
})

Upvotes: 0

Views: 1846

Answers (2)

charlietfl
charlietfl

Reputation: 171669

Use JSON.parse() to convert from string to array:

var arr = JSON.parse('[[1,2,3],[1,2,3]]')
console.log(arr[0])

Upvotes: 1

Daniel AG
Daniel AG

Reputation: 350

You can convert it to a multidimensional array by parsing it as a JSON string:

 var parsedArray = JSON.parse(yourString);

Upvotes: 0

Related Questions