Hoslack
Hoslack

Reputation: 105

Return an array as a string JavaScript

I want return an array as a string in my function.
Example:

return [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

should be returned as "[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]" which is a string. I don't want to use .join() because it removes the [] brackets.

Upvotes: 0

Views: 368

Answers (2)

asmmahmud
asmmahmud

Reputation: 5054

Return your array after JSON.stringify it. like that:

var arr = [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]];
return JSON.stringify(arr);

It will convert the array object into string.

Upvotes: 1

Nisarg Shah
Nisarg Shah

Reputation: 14561

You can use JSON.stringify - it converts JS objects into JSON strings.

console.log(JSON.stringify([[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]));

Upvotes: 5

Related Questions