0

Array:

myArr = [[1, 2, 3], [4, 5, 6]]

Expected output:

newArr = "[[1, 2, 3], [4, 5, 6]]"

I have tried:

myArr.toString()
String(myArr)
myArr = `${myArr}`

What I got by doing the above methods:

'1,2,3,4,5,6'
1

3 Answers 3

3

I gather what you would like to achieve is more or less serialization. We could use JSON.stringify in JavaScript to serialize an Array.

const array = [[1, 2, 3], [4, 5, 6]];
const serializedArray = JSON.stringify(arr));

To deserialize the Array, JSON.parse could be used.

const originalArray = JSON.parse(serializedArray));
Sign up to request clarification or add additional context in comments.

Comments

1

JSON.strinify is what you need. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

const myArr = [[1, 2, 3], [4, 5, 6]];
const myArrString = JSON.stringify(myArr);

console.log(`Here is my string: ${myArrString}`);

Comments

0

use JSON.stringify(myArr);

you can find out more on below link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.