This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Parsing string as JSON with single quotes?
(10 answers)
Closed last month.
i have a string containg an array. i can convert it using loop over string with different conditions but that will be slow. so is there any direct way to convert string which has array in it to an actuall array?
my javascript string looks like this
arr_str = '["value1", "value2", "value3"]'
i want to convert it in array something like this
arr = ["value1", "value2", "value3"]
Related
This question already has answers here:
Safely turning a JSON string into an object
(28 answers)
Closed 1 year ago.
I have a string that is in a format of a Array, but I need it to be a Array and it has to be a string.
Example Of Data (string):
[{"itemName": "LimitedName", "itemName": "ID", "itemClassName": "ClassName", "MaxAllowed": 1000}]
I need it to become an actual array and be able to add more columns to it as well.
Can someone help me?
You can use JSON.parse(someString)
This question already has answers here:
Parsing string as JSON with single quotes?
(10 answers)
Closed 2 years ago.
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.
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, '"'))
This question already has answers here:
How to convert an array like string to array in node.js?
(4 answers)
Closed 3 years ago.
I need to convert raw String to json array in javascript below the my logic
Original:
"[template1,template2]";
Exception:
"["template1","template2"]";
use slice to retrieve the text between the square brackets and then use split.
const input = "[template1,template2]";
const arr = input.slice(1, -1).split(',');
console.log(arr);
This question already has answers here:
Convert array to JSON
(12 answers)
Closed 4 years ago.
I need to convert list of strings
const ar: string[] = ['str1', 'str2']
to a string which contains ar container with square brackets [] and quotes "
const str: string = `["str1", "str2"]`
how to do it in proper way?
The format you want is a JSON array string. So use the JSON object's stringify function.
JSON.stringify(["some", "array", 123]);
This question already has answers here:
Convert string with commas to array
(18 answers)
Closed 5 years ago.
var array = ["Charizard,Pikachu,Glalie,Delfox"];
I tried to use the split() method except that only affects string variables and not arrays.
My ultimate goal is to make array = ["Charizard","Pikachu","Glalie","Delfox"];
You can retrieve the value of the array an split it using as delimiter the comma ','
var array = ["Charizard,Pikachu,Glalie,Delfox"];
console.log(array[0].split(','))