marcamillion marcamillion - 1 month ago 8
jQuery Question

If I have an array of values, how do I join some together separated by a dash?

Say I have an array that looks like this:

array1 = button.attr('id').split('-');
["slider", "step", "value", "speed", "rebecca", "nitzsche", "st", "george", "s", "college"]


What I want to do is to take every element after
speed
, so everything after
array1[3]
to the end, and mush it all into 1 string separated by a
-
so it looks like this:
rebecca-nitzsche-st-george-s-college
.

How do I do this?

Answer

You can simply use slice and then join, like so:

var array = ["slider", "step", "value", "speed", "rebecca", "nitzsche", "st", "george", "s", "college"];
array.slice(4).join('-')

// It will return => "rebecca-nitzsche-st-george-s-college"