Categories
JavaScript Answers

How to convert string array representation back to an array with JavaScript?

Spread the love

Sometimes, we want to convert string array representation back to an array with JavaScript.

In this article, we’ll look at how to convert string array representation back to an array with JavaScript.

How to convert string array representation back to an array with JavaScript?

To convert string array representation back to an array with JavaScript, we can use JSON.parse.

For instance, we write:

const s = '["item1", "item2", "item3"]'
const a = JSON.parse(s)
console.log(a)

to call JSON.parse with s to convert s back to an array.

Therefore, a is ["item1", "item2", "item3"].

Conclusion

To convert string array representation back to an array with JavaScript, we can use JSON.parse.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *