2

I have an array

let arr = [12,12,43,53,56,7,854,3,64,35,24,67] 

i want the result back as string

let strArr = "12,12,43,53,56,7,854,3,64,35,24,67" 

Please some one suggest me any solution

1
  • just make arr + "" Commented Apr 10, 2017 at 9:19

5 Answers 5

1

You can use toString() method:

let arr = [12,12,43,53,56,7,854,3,64,35,24,67]; arr = arr.toString(); console.log(arr); console.log(typeof arr);

You can read more about this here.

Sign up to request clarification or add additional context in comments.

2 Comments

Felicitari for this nice solution. +1
@Alexandru-IonutMihai, multumesc :). Same goes for your answer. :)
1

One solution is to use join method.

The join() method joins the elements of an array into a string, and returns the string.

let arr = [12,12,43,53,56,7,854,3,64,35,24,67] let strArr = arr.join(); console.log(strArr);

Comments

1

Use Array.prototype.join().

The join() method joins all elements of an array (or an array-like object) into a string.

var a = [12,12,43,53,56,7,854,3,64,35,24,67]; a.join(); // '12,12,43,53,56,7,854,3,64,35,24,67' 

Comments

1

JS type coercion is sometimes useful.

var arr = [12,12,43,53,56,7,854,3,64,35,24,67], strArr = arr + ""; // <- "12,12,43,53,56,7,854,3,64,35,24,67" 

Comments

1

Solution to this would be to use join()

let arr = [12,12,43,53,56,7,854,3,64,35,24,67] let strArr = arr.join(); 

Second you be to use toString()

 let arr = [12,12,43,53,56,7,854,3,64,35,24,67] let strArr = arr.toString(); 

Because you want to join by a comma, they are basically identical, but join allow you to chose a value separator.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.