Javascript Turn string into array [duplicate] - javascript

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 7 years ago.
I want to turn this into an array
"["65747add-afd2-45b5-92e0-150bbe40e6d9", "9c247ea5-6b81-4f47-a50c-42367dedd50b", "c1555363-aca9-4e04-8844-e0180397c72e"]"
I am getting it from the page like this:
$('#layout-uuids').text()
Is there away to just get it as an array or do I need to turn the string into an array somehow?
Thanks!

That string looks like JSON. If it is indeed JSON, all you need is JSON.parse():
var someArray = JSON.parse($('#layout-uuids').text());

Related

Parsing a stringified array to an array [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 9 months ago.
I have a component that throws the following in the backend
memberSince: [ '["2022-05-05T08:13:07.551Z","2022-06-01T08:13:07.551Z"]' ]
Is there a way to un-stringify the array content like memberSince[0].parse() or something
You can use JSON.parse()
const data = {memberSince: ['["2022-05-05T08:13:07.551Z","2022-06-01T08:13:07.551Z"]']}
const result = JSON.parse(data.memberSince)
console.log(result)

Text Array to Array in Javascript [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 3 years ago.
I need convert from data = "[[0,1], [1,1]]" to data = [[0,1], [1,1]] in javascript, remove double quotes . It's possible?
Thanks
Just use the JSON object:
data = JSON.parse("[[0,1], [1,1]]"); // data === [[0,1], [1,1]]

serialized string to form [duplicate]

This question already has answers here:
How to convert URL parameters to a JavaScript object? [duplicate]
(34 answers)
Closed 3 years ago.
I have serialized form in string such as:
"name=Michael&surname=Davies&multiple=selection1&multiple=selection3"
How can I convert this string to object and then change values in form, or directly change values in form with this string? I have several forms in one html page and each string represent values in one form.
Thank you.
Try using below to get an array. Then u can put them in to a JSON object by traversing through the array. May be not the best solution.
$("#form").serializeArray();

Reading Json slash in Javascript [duplicate]

This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 7 years ago.
I'm reading a JSON file through javascript. I'm having trouble getting today/_text because of the forward slash. I can get today successfully by doing: {{hours.results[0].today}}. How would I get today/_text? I've tried:
today\/_text
today/\_text
today//_text
today\\/_text
{"offset":0,"results":[{"today/_text":"Today:YES","today/_source":"/hours/1","today":"2,3,4"}]}
hours.results[0]["today/_text"] should do the trick!
hours.results[0] returns an object that has that as a key, making that the easiest way to access the property in question.

Convert JSON string to Javascript array [duplicate]

This question already has answers here:
Convert a multidimensional javascript array to JSON?
(9 answers)
Closed 2 years ago.
I have an array of arrays which I have planted in the DOM, so that I can reuse it at a later stage to transfer to the server:
[["1","aaaaaa","1"],["2","bbbbbbb","2"],["3","ccccccc","3"]]
If I would like to convert it back into a Javascript array, how would I go about doing this?
var obj = $.parseJSON('[["1","aaaaaa","1"],["2","bbbbbbb","2"],["3","ccccccc","3"]]')
Assuming jquery is ok to use because of tag.
If the browzer has the JSON object then
JSON.parse(string);
or if you have jQuery
$.parseJSON(string);
var array = JSON.parse(my_JSON)
jQuery.parseJSON()
var myArr = $.parseJSON(myJsonString);

Categories

Resources