convert string to an array using JQuery [duplicate] - javascript

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)

Related

How we can convert an string of array to Array in JavaScript? [duplicate]

This question already has answers here:
Convert string array representation back to an array
(3 answers)
Closed 7 months ago.
Example :
Input = '[1,2,3]' // typeof string
Expected Output = [1,2,3] //typeof Array
Use JSON.parse to parse an string into an array/object
let myArray = JSON.parse(input)

How to convert an array to string [duplicate]

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

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 a Substring JAVASCRIPT? [duplicate]

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(")")))
})

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

Categories