简体   繁体   中英

Converting Javascript Array into String for Function

I would like to use an array i made to run .superslides function on

$(function() {
  $('#slides-1, #slides-2').superslides({
    hashchange: false
  });
});

here it is with the array i want to use

$(function() {
    $(slideShowArr).superslides({
        hashchange: false
    });
});

and, just in case, here is how the array was made

$(".slideshow").each(function(i, el){
    el.id = 'slides-' + (i + 1);
    slideShowArr.push('#' + $(this).attr('id'));
});

so i need to pass slide show id's to .superslides function

Use the join method of Array class to achieve this.

$(function() {
    $(slideShowArr.join(",")).superslides({
        hashchange: false
    });
});

Not sure whether you really want this:

$(function() {
    var slideShowArr = [];
    $(".slideshow").each(function(i, el){
        el.id = 'slides-' + (i + 1);
        slideShowArr.push('#' + $(this).attr('id'));
    });
    $(slideShowArr.join(", ")).superslides({
        hashchange: false
    });
});

because it makes absolutely no sense to iterate over a collection giving each item an id and then making a new selection from all the generated ids - just use the collection you already have! Better:

$(function() {
    $(".slideshow").prop("id", function(i) {
        return 'slides-' + (i + 1);
    }).superslides({
        hashchange: false
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM