0
Q:

array to string javascript

['tom', 'dick', 'harry'].join(', ').replace(/, ([^,]*)$/, ' and $1')
> "tom, dick and harry"
1
var myArray = ['no','u'];
var myString = myArray.toString();
8
const arr = [1, 2, 'a', '1a'];
const str = arr.toString();
console.log(str); //> "1,2,a,1a"
5
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = fruits.toString();
4
// Defining an array with most datatypes ( objects have to be converted to string with JSON.stringify(obj) )
var myArray = ['String',"String",1,2.5, false, NaN, undefined]; 
// Convert the array into a string using the JS function toString();
var myString = myArray.toString();
// Logging it on the console
console.log(myString) // "String,String,1,2.5,false,NaN," (undefined values cannot be converted to strings)
1
var array = [1,2,3]

for(var i = 0; i < array.length ; i++){
    console.log(array[i])
}
2
['h', 'e', 'y'].join('')
// hey
2
//comma separated string into array
var colorsString="red,green,blue";
var colorsArray=colorsString.split(","); //split colorsString into array;
//console.log(colorsArray); ["red", "green", "blue"]
0
<array>.join(<splitting character(s)>);
8
var address = "foo";
var city;
var state = "bar";
var zip;

text = [address, city, state, zip].filter(Boolean).join(", ");
console.log(text)
1
//Use array.join(separator) //
const myArray = [1, 2, 3, 4, 5, 6];
console.log(a.join('/'));
//output : 1/2/3/4/5/6
const myArray = ['g', 'o', 'o', 'g', 'l', 'e'];
console.log(a.join(''));
//output : 'google'
// Source : https://www.geeksforgeeks.org/javascript-array-join-method/#:~:text=Below%20is%20the%20example%20of%20the%20Array%20join()%20method.&text=The%20arr.,is%20a%20comma(%2C%20).

//Other function : array.toString() //
const array1 = [1, 2, 'a', '1a'];
console.log(array1.toString());
// output: "1,2,a,1a"
// Source : https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/toString
0

New to Communities?

Join the community