Search code examples
javascriptarraysloopsdynamic-url

JS - make dynamic url from all array data (array length is not fixed)


I am trying to make a dynamic url from all array data. Now I have come to this part and I don't have idea how to resolve this. Can You please help?

    //Get image
    function get_image(img_arg){

        var length = img_arg.length;

        for (x = 0; x < length; x++){

            //I would like to make url like this:
            //var url = img_arg[0] + '-' + img_arg[1] + '-' + img_arg[2]...till the max;
        }


        console.log(url);
    }

Thanks!


Solution

  • Use join() function on your array and pass - as parameter. It concatenates all items with passed parameter and returns you a string.

    As an example you can see

    var img_arr = [1,2,3,4,5,6,7,8,9];
    var arr = img_arr.join('-');
    
    console.log(arr);