convert string to map in javascript [duplicate] - javascript

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

Related

How do i convert this string into an array? [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Safely turning a JSON string into an object
(28 answers)
Closed 3 years ago.
I need to convert this string into a an array into the format shown below.
itemString
[{"_id":"1e12345","a":"abc","b":def","c":ghi"},{"_id":"1e678910","a":"xzx","b":sed","c":ert"}]
var item=itemString.split("},{").map(function(i){
return i
})
console.log(item)
what i need to get from from console.log(item)
0:{_id:"1e12345",a:"abc",b:"def",c:"ghi"}
1:{_id:"1e678910",a:"xzx",b:"sed",c:"ert"}
what i get from console.log(item)
0:"[{"_id":"1e12345","a":"abc","b":"def","c":"ghi""
1:""_id":"1e678910","a":"xzx","b":"sed","c":"ert"}]"

Convert raw string to array [duplicate]

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

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

parse string and store elements to an array [duplicate]

This question already has answers here:
How to use split?
(4 answers)
Closed 9 years ago.
I have a string likes below
a_b_c_d
I hope to decode it to an array t as
t[0]->a
t[1]->b
t[2]->c
t[3]->d
Just wonder if there ia function of javascript can parse the string directly.
var string = "a_b_c_d";
var t = string.split('_');
DEMO FIDDLE
Just split the string
var t = "a_b_c_d".split("_");

get query string value using javascript [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
Given the following string/query string:
ajax/hovercard/hovercard.php?id=100000472545907&extragetparams=%7B%22hc_location%22%3A%22stream%22%7D
What is the best way to extract the id?
Try this:
yoururl.match(/id=(.*)&/)[1]
Fiddle
params = location.search.substring(location.search.indexOf('id')).split('&')[0]
id = params.substr(3)
If it is always shipped in this exact order - then
url.match(/\d+/)
otherwise
url.match(/id=(\d+)/)

Categories

Resources