How to convert string separated by commas to array? [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert JS object to JSON string
Store comma separate values into array
I have a string containing values separated with commas:
"1,4,5,11,58,96"
How could I turn it into an object? I need something like this
["1","4","5","11","58","96"]

This will convert it into an array (which is the JSON representation you specified):
var array = myString.split(',');
If you need the string version:
var string = JSON.stringify(array);

In JSON, numbers don't need double quotes, so you could just append [ and ] to either end of the string, resulting in the string "[1,4,5,11,58,96]" and you will have a JSON Array of numbers.

make it an array
var array = myString.split(',');

Related

Encode array contents with base64 [duplicate]

This question already has answers here:
How can you encode a string to Base64 in JavaScript?
(33 answers)
Closed 2 years ago.
I want to encode my array contents with base64 if possible in javascript (and then decode later).
Example:
var array = ["stack", "overflow"]
// base64.encode(array)
Code:
var array = ["stack", "overflow"]
array.map(btoa);
In order to use the well-known function btoa, you'll first have to convert your array to string, in such a way that you can reverse the operation. JSON would be the string format to go for.
So to encode do:
base64 = btoa(JSON.stringify(array))
To decode do:
JSON.parse(atob(base64))

Sorting strings in javascript [duplicate]

This question already has answers here:
How do you reverse a string in-place in JavaScript?
(57 answers)
Closed 3 years ago.
I would like to sort strings in javascript containing comma separated values in different e.g.
var Str = "8,0,2,10"
I want to sort it like below example form the last one to first one:
var NewStr = "10,2,0,8"
You can convert string to array using split() and reverse the array element using reverse() and then convert result to string again using join() like this:
var Str = '8,0,2,10';
var dif = Str.split(',').reverse().join(',');
console.log(dif);

How to get numbers in a string separated with commas and save each of them in an Array in Javascript [duplicate]

This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 5 years ago.
I have a string of ID's wherein they're being separated with commas.
For example, the string is:
"15,14,12,13"
How can I extract the numbers/id's from this string and save each of them in a JSON or array to be something like this
Array: {
15,
14,
12,
13
}
I don't know how it's done using regex or string manipulation. Please advice.
use split & map & parseInt methods.
var numbers="15,14,12,13";
var result=numbers.split(',').map(function(number){
return parseInt(number);
});
console.log('convert '+JSON.stringify(numbers)+" to array:"+JSON.stringify(result));
Use eval method
var numbers="15,14,12,13";
var result=eval("["+numbers+"]");
console.log('convert '+JSON.stringify(numbers)+" to array:"+JSON.stringify(result));

Can i further break an array in javascript [duplicate]

This question already has answers here:
How to get character array from a string?
(14 answers)
Closed 6 years ago.
Suppose I have the following array-
var x= ["hello"];
Can i further break it into a character array like this one-
var x_character= ["h","e","l","l","o"];
if not, can you tell me how to know the character length of the x array..
Yes, use the .split() method:
var x = ["hello"]
x[0].split("")
Returns the array ["h","e","l","l","o"]. The "" argument means to split the string at each empty substring.
Create a new empty array, iterate through the original array and push each character into the new array. To get the length of the original array - use "x.length" (ie: "var arrayLength=x.length";)
var x = ["hello"];
var x_character=[];
for(i=0;i<x.length;i++)
{
var x_character.push(x[i])
}

get number only in javascript [duplicate]

This question already has answers here:
How can I extract a number from a string in JavaScript?
(27 answers)
Closed 9 years ago.
I have a string likes
AA-12,AB-1,AC-11,AD-8,AE-30
I want to get number only from this string likes
12,1,11,8,30
How can I get this using JavaScript ? Thanks :)
Use a regex, eg
var numbers = yourString.match(/\d+/g);
numbers will then be an array of the numeric strings in your string, eg
["12", "1", "11", "8", "30"]
Also if you want a string as the result
'AA-12,AB-1,AC-11,AD-8,AE-30'.replace(/[^0-9,]/g, '')
var t = "AA-12,AB-1,AC-11,AD-8,AE-30";
alert(t.match(/\d+/g).join(','));
Working example: http://jsfiddle.net/tZQ9w/2/
if this is exactly what your input looks like, I'd split the string and make an array with just the numbers:
var str = "AA-12,AB-1,AC-11,AD-8,AE-30";
var nums = str.split(',').map(function (el) {
return parseInt(el.split('-')[1], 10);
});
The split splits the string by a delimiter, in this case a comma. The returned value is an array, which we'll map into the array we want. Inside, we'll split on the hyphen, then make sure it's a number.
Output:
nums === [12,1,11,8,30];
I have done absolutely no sanity checks, so you might want to check it against a regex:
/^(\w+-\d+,)\w+-\d+$/.test(str) === true
You can follow this same pattern in any similar parsing problem.

Categories