How to convert an array to string [duplicate] - javascript

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

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 get arrays from sting? [duplicate]

This question already has answers here:
How can I convert a comma-separated string to an array?
(19 answers)
Closed 2 years ago.
I want to get an array from strings
const id = 1,8,10
output = ['1','8','10']
Any idea how to do it?
If the numbers are separated by , u can split the string like this:
const id = '1,8,10'
const output = id.split(',')
console.log(output)
>>> ["1", "8", "10"]

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

convert string to an array using JQuery [duplicate]

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)

Categories