Skip to content

Instantly share code, notes, and snippets.

@longfellowone
Last active December 31, 2018 02:52
Show Gist options
  • Save longfellowone/44a2322528396d0e018b8871e665c6ee to your computer and use it in GitHub Desktop.
Save longfellowone/44a2322528396d0e018b8871e665c6ee to your computer and use it in GitHub Desktop.
Javascript bold or replace character in string at index position
// Working example
// https://codesandbox.io/s/lr5zjxrn67
// Method #1
function replaceAt(string, indexArray) {
let newString = [...string];
for (let i = 0; i < indexArray.length; i++) {
newString = Object.assign(newString, {
[indexArray[i]]: <b>{newString[indexArray[i]]}</b>
});
}
return newString;
}
//Method #2
function replaceAt(string, indexArray) {
const newString = [...string];
return newString.map(i => {
indexArray.map(i => {
return (newString[i] = <b>{newString[i]}</b>);
});
return i;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment