const toReplace = [ { key: "{user1}", value: member.user.toString() }, { key: "{user2}", value: invite.inviter.toString() }, ]; toReplace.forEach((r) => { welcomeFormat = welcomeFormat.toLowerCase().replace(r.key.toLowerCase(), String(r.value)); });
Hey guys. Currently I’m trying to replace a specific string with lowercase, to reduce the chance of userinput error.
However, the following code replacing the WHOLE message tolowercase. How would i go about only replacing contents inside of {}
to lowercase?
Advertisement
Answer
It is better to do it with map
(not forEach
).
const toReplace = [ { key: "{user1}", value: member.user.toString() }, { key: "{user2}", value: invite.inviter.toString() }, ]; const newToReplace = toReplace.map((r) => { return { ...r, key: r.key.toLowerCase(), }; });
this way, you can keep both arrays but if you want to replace the original array with the new one, you can name it toReplace
to replace it with the new one.