The Duy
The Duy

Reputation: 9

Convert array string to array for mapping data

Here is my problem

const data = "[\"https://i.imgur.com/PAYXC2n.jpg\",\"https://i.imgur.com/bEfjjxA.jpg\"]";

I want to convert this string to array, expected result will be :

res =["https://i.imgur.com/PAYXC2n.jpg","https://i.imgur.com/bEfjjxA.jpg"]

I use it for mapping data in React JS project, so i need to convert this string to array.

Upvotes: 0

Views: 62

Answers (2)

Esteban Cabrera
Esteban Cabrera

Reputation: 56

Do it: const dataArr = JSON.parse(data);. This will parse as to object, which is your array (With out "")

Upvotes: 2

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

Just parse it from JSON:

const
  data = "[\"https://i.imgur.com/PAYXC2n.jpg\",\"https://i.imgur.com/bEfjjxA.jpg\"]",
  parsed = JSON.parse(data);

console.log(parsed);

Upvotes: 2

Related Questions