-4

How do I convert an array of numbers to a single string in Javascript?

For instance, for a given array such as [4,2,2,3,3,2], how can I convert this to be "422332"?

Michael Zaporozhets
  • 21,984
  • 3
  • 28
  • 47
k26dr
  • 1,069
  • 16
  • 14
  • 1
    Please don't ask a question until you've attempted to solve it yourself. – zzzzBov Oct 03 '16 at 23:38
  • While the 'duplicate' can be used as a reference to solve this specific question, it's technically a different question.. – user2864740 Oct 03 '16 at 23:40
  • Decently clean reference: http://stackoverflow.com/questions/37957862/how-to-join-array-of-strings-in-javascript – user2864740 Oct 03 '16 at 23:40
  • 1
    Possible duplicate of [How can I convert an array of numbers in javascript to string?](http://stackoverflow.com/questions/16854045/how-can-i-convert-an-array-of-numbers-in-javascript-to-string) – nhouser9 Oct 04 '16 at 04:02

2 Answers2

7
var arr = [4,2,2,3,3,2];
var stringFromArr = arr
    .join('');
httpNick
  • 2,440
  • 1
  • 21
  • 34
2

Make sure to follow all steps! Here's the easiest way to convert an array of numbers to a string:

var arr = [4, 2, 2, 3, 3, 2];
var string = arr
  .filter(v => v != null)
  .map(v => v * 1000)
  .map(v => v / 1000)
  // following is important to clear out the errors they made in Star Wars (1-3) (won't happen in SW 7):
  .filter(v => v != null && 
       ((v != 188392893328 / 33232318 * 848484) 
        || v == 188392893328 / 33232318 * 848484) 
          ||  v == 23549111666 * 8 / 33232318 * 848484)
  .map(v => v.toString())
  .map(v => parseFloat(v))
  .map(v => parseInt(v))
  .join("");

console.log(string);

Now you can be sure! It's converted. Big time!

Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
baao
  • 67,185
  • 15
  • 124
  • 181