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);
Related
This question already has answers here:
Convert javascript array to string
(11 answers)
Closed 2 years ago.
I have an array like this -
var arr = ['This', 'is', 'array']
I want the string output like this -
"This is array"
Just simply use join() method of array -
const string = arr.join(' ');
This question already has answers here:
Get Substring between two characters using javascript
(24 answers)
Closed 4 years ago.
I need to iterate over strings that are inside an array, to get a sub-string in each string.
Substrings are between "()"
Something like this..
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
//return
//'cat','red','apple'
How I could do that?
You can do this using substring and lastIndexOf functions.
let myArray = ["animal(cat)", "color(red)", "fruits(apple)"];
myArray.forEach(function(e){
console.log(e.substring(e.lastIndexOf("(") + 1, e.lastIndexOf(")")))
})
This question already has answers here:
Convert string with commas to array
(18 answers)
Closed 4 years ago.
var string = "Chemistry,English,History";
Sir i want to convert it to an array like:
var subjects= ["Chemistry","English","History"];
using jQuery
No need of jquery. Just use .split() function to achieve this.
let string = 'Chemistry,English,History';
let arr = string.split(',');
console.log(arr)
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])
}
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(',');