var a = [6, 7, 8, 14, 15, 16, 20, 21]; var b=[]; for (var i = 0; i < a.length-1; i+=3) { b[b.length]=a[i]+' - '+((a[i+2])? a[i+2]: a[i+1]); }; alert(b);