JSFiddle - React, Tailwind, and code Playground
JavaScript
if (!String.prototype.splice) {
/**
* {JSDoc}
*
* The splice() method changes the content of a string by removing a range of
* characters and/or adding new characters.
*
* @this {String}
* @param {number} start Index at which to start changing the string.
* @param {number} delCount An integer indicating the number of old chars to remove.
* @param {string} newSubStr The String that is spliced in.
* @return {string} A new string with the spliced substring.
*/
String.prototype.splice = function(start, delCount, newSubStr) {
return this.slice(0, start) + newSubStr + this.slice(start + Math.abs(delCount));
};
}
var str = " absolutely pie";
var indices = [0, 12, 16]; // indices where I want to insert
var things = ["I", "love", 1]; // things I want to insert
for(var i = 0; i < indices.length; i++) {
str = str.splice(indices[i], 0, things[i]);
}
console.log(str); // supposed to print out "I absolutely love pie1"