parse string and store elements to an array [duplicate] - javascript

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("_");

Related

How to split a string using comma? [duplicate]

This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 2 years ago.
I have a string like this
var assignments= "NAME1,NAME2,NAME3,NAME4,NAME5,NAME6"
how do i print only first three strings like this "NAME1,NAME2,NAME3" in JavaScript
You can do this way
let string= "NAME1,NAME2,NAME3,NAME4,NAME5,NAME6".split(',')
let output = string.splice(',',3).join(',')
console.log(output)
Using standard Array functions you can do what you want. The code below shows the various stages, though you can write it more concisely as one line if you like.
var assignments= "NAME1,NAME2,NAME3,NAME4,NAME5,NAME6"
var splitAssignements = assignments.split(",")
var firstThreeArray = splitAssignements.slice(0,3)
var firstThreeString = firstThreeArray.join(",")
console.log(firstThreeString)

how to convert "12A,13B,14C" to [12A,13B,14C] in javascript [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 5 years ago.
I have a string like below.
sections = "12A,13B,14C"
But I need [12A, 13B, 14C] for future use. How to create array from above string?
You have to use split method for that to split your string data into array values, have a look to updated code
var sections = "12A,13B,14C";
sections = sections.split(',');
console.log(sections);
Hope this will help! Just use split method of JS.
sections = "12A,13B,14C"
let array = sections.split(',');
console.log(array)

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

JavaScript / jQuery: how to split string with multiple values [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method

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