Senchu Thomas
Senchu Thomas

Reputation: 568

How to parse multidimensional array in string format back to multidimensional array

I am using angular5.I have a questionOrderList which is a string which contains multidimensional array in string format. What I need to do is to parse the array in string format back to multidimensional array type. Currently I have used the JSON.parse method my code looks like.

    console.log("qorder service : "+questionOrderList)
    console.log("qorder service parsed:" +JSON.parse(questionOrderList))

The output I am getting is

qorder service : [[3290],[3287],[3289,3293],[3295]]
qorder service parsed: 3290,3287,3289,3293,3295

But in case of array with one row it is correct

qorder service : [[3290,3287,3289,3293,3295]]
qorder service parsed: 3290,3287,3289,3293,3295

Upvotes: 0

Views: 79

Answers (1)

Ayush Gupta
Ayush Gupta

Reputation: 9285

What you are going is correct, see this:

var questionOrderList = "[[3290],[3287],[3289,3293],[3295]]";
var parsedList = JSON.parse(questionOrderList);

console.log(parsedList)

The actual issue is how JS interprets string concatination with arrays. It implicitly calls .join(',') on the array as demonstrated here:

var questionOrderList = "[[3290],[3287],[3289,3293],[3295]]";
var parsedList = JSON.parse(questionOrderList);
console.log(parsedList);
console.log("" + parsedList);
console.log(parsedList.join(','))

Upvotes: 1

Related Questions