This may be the silliest question I'm asking But I already wasted nearly 30 mins to get this done and thought to ask from you guys.
What basically this code needs to do is just replace the str value if it's in the list (Only if whole str matched with the key), otherwise, return the str. However, I'm getting undefined if it wasn't on the list.
NOTE: I don't want it to wrap the result inside an if/else statement and if undefined return the str. Any way to do it with a simple tweak?
var list = {
'a': 'aaa',
'b': 'bbb',
'aa': 'ccc',
'bb': 'ddd',
}
var str = 'a'
var newStr = str.replace(new RegExp(str), (a) => list[a])
// This is how it should be if it wasn't a 'var'. But with a `var` ?
// var newStr = str.replace(new RegExp(/str/), (a) => list[a])
console.log(newStr)
newStr = list[str] ?? stris all you need. That's why I wonder about the replacement.||right?""you'd get backstrand I don't think it's intended.