Use Object.entries to get a list of pairs of items and then run a forEach with the string’s replace method on it make sure to use a regex because I think it only replaces one instance of the word if you just striahgt up pass a string. I’d also really advise against naming a string string, I’ll assume you’re doing that just for the example
The way you’d do what @javaarchive suggested is something like this, with lots of comments so you can actually learn something from it:
const wordReplaces ={
apple: 'lion',
banana: 'dog'
};
let string = "apple is a banana";
Object.keys(wordReplaces) // get the keys of the words as an array i.e. ["apple", "banana"]
.forEach( // loop over the keys
key => {
let wordRegex = new RegExp(`\b${key}\b`, "gi"); // \b is a word boundary so we don't replace things in the middle of words - remove those if you'd like. The "gi" means to search globally and case-insensitively
string.replaceAll(wordRegex, wordReplaces[key]); // replace every occurence of the word with the new word
})
That won’t quite work, because you can’t re-declare replaced and also because each time you’re going back to the original to do the substitution whereas you want all of the substitutions to be … cumulative: