-
-
Save longfellowone/44a2322528396d0e018b8871e665c6ee to your computer and use it in GitHub Desktop.
Javascript bold or replace character in string at index position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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