Search code examples
javascriptarrayssplice

Splice method doesent insert element


I have an issue where i want to add a char at a specific index in a string. Its supposed to take a http... address and add "s" at the end of http. After splice console log doesn't even print "url". What am I doing wrong? const url = hero.comics.collectionURI.split("").splice(5, 0, "s").join("");


Solution

  • splice returns the deleted elements as an array, not the array it was called on.

    let arr = hero.comics.collectionURI.split("");
    arr.splice(5, 0, "s");
    const url = arr.join("");