Convert raw string to array [duplicate] - javascript

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);

Related

How to convert an array of type String to an array object in javascript [duplicate]

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, '"'))

js list of strings to string [duplicate]

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]);

what method can I use to separate strings inside array into individual arrays (using JavaScript)? [duplicate]

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(','))

Convert each value in the array to string [duplicate]

This question already has answers here:
Convert integer array to string array in JavaScript
(11 answers)
Closed 6 years ago.
I have this array
var p = [0,1,2,3,4];
and I want to convert each value in the array to string like
var p = ['0','1','2','3','4'];
any ideas, help please?
You can convert number to string simply by prepending empty string to it.
p = p.map(function(e){return ""+e});
All you need to do is pass the Number value to the String function to get a string out of a Number.
[0,1,2,3,4].map(function(value) { return String(value); });

convert string to map in javascript [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 7 years ago.
how to convert the following string to map so that i can access the values
data.message ="{"id":"60653","key":"project1","self":"http://127.0.0.1:321/rest/api/2/issue/project1"}"
if your string is a valid JSON just use the JSON.parse function:
var str = '{"id":"60653","key":"project1","self":"http://127.0.0.1:321/rest/api/2/issue/project1"}'
var mydata = JSON.parse(str);
Use JSON.parse
Here's the corresponding MDN documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Categories

Resources