1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /** * Simple function that acts like String.join(), except it allows for a custom final joiner. This * is helpful in situations where you want to join an array in human readable form, e.g. * var arr = ["one", "two", "three"]; * alert(arr.customJoin(", ", " and ")); // alerts "one, two and three" */ Array.prototype.customJoin = function(glue, finalGlue) { // if there's at least one element, pop off the LAST item var lastItem = null; if (this.length > 0) lastItem = this.pop(); // now, if there's still 1 or more items left, join them with the glue if (this.length > 0) { var str = this.join(glue); str += finalGlue + lastItem; } // otherwise there was only one item: just return the string else str = lastItem; return str; } |
Recent Comments