0

I have a string containing an array, and want to extract the array and play with its objects.

var arrayString = "[{'name': 'Ruwaida Abdo'}, {'name': 'Najlaa Saadi'}]";

Basically, I am dealing with a JSON file in which some properties are in fact arrays but stored as strings. Hence, I need to deal with them as strings and convert them to array to use their objects.

Abu Zaid
  • 15
  • 5
  • use `JSON.parse(arrayString)`. Read [Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse). Also don't forget to surround it with try-catch block (in case malformed json string) – Quyet Nguyen May 14 '20 at 03:42
  • Please store them as a valid JSON string. It should have double quotes around keys and values. – adiga May 14 '20 at 03:51

1 Answers1

1

What you have here is a JSON string. You can parse it to get the object / array:

var array = JSON.parse(arrayString)

Edit: I see your JSON string has single quotes. You need to replace all of them with double quotes before parsing:

JSON.parse(arrayString.replace(/'/g, '"'))
chiragrtr
  • 902
  • 4
  • 6