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]);
Related
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"]
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 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(','))
This question already has answers here:
convert string into array of integers
(12 answers)
Closed 5 years ago.
How do i break down this " 01|15|59, 1|47|6, 01|17|20, 1|32|34, 2|3|1 " string into 5 integer arrays ?.
For example:
01|15|59 becomes [01,15,59]
1|47|6 becomes [1,47,6]
var result = string.trim().split(", ").map(el=>el.split("|").map(n=>+n))
this splits the string into an array of these groups ( yet as string )
"1|2, 3|4" => ["1|2","3|4"]
then maps this array to a new array containing the strings splitted and converted to numbers:
=> [[1,2],[3,4]]