Javascript – Pretty Array to Human Readable String

To take a JS array like [“item1”, “item2”, “item3″….] into a nice human readable string of “item1, item2 & item3” with all the first items getting comma separated and the final one with an ampersand use:

resultString = array.length > 1 ? array.slice(0,-1).join(', ') + ' & ' + array[array.length -1] : array[0]

Codemonkey of Node.js, Django, PHP and all things HTML5 Solving tech challenges, one bit at a time.

Posted in javascript, servers Tagged with: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*