Also consider .replace(/\*\*\*/g, '0') if you want to replace multiple occurrences of ***. (The below code only replaces the first occurrence in each string.)

stuff= ["uyuuyu", "76gyuhj***", "uiyghj", "56tyg", "juijjujh***"];

for(var i = 0; i < stuff.length; i++)
{
    stuff[i] = stuff[i].replace('***', '0');
}

console.log(stuff);

Note that there's no need to check indexOf. If the substring isn't present, the replace just doesn't change anything, so you can just apply the replace to every string.

Answer from user94559 on Stack Overflow
Discussions

javascript - Replace characters in an Array - Stack Overflow
I am trying to replace characters in an array-string, but it is not working. Basically you enter a word, then the user has to guess it while typing only 1 character. If the character is in the word... More on stackoverflow.com
🌐 stackoverflow.com
March 23, 2017
How to replace a letter in an array?
Please help me how to change the letter in the array without creating a new array ? More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
March 10, 2023
javascript - Replace character in array of objects - Stack Overflow
If you want to replace characters only in the values of the objects in array, you can try following code. More on stackoverflow.com
🌐 stackoverflow.com
Replace certain character on string from an array of string in javascript - Stack Overflow
I have a string like this const text = 'hello ___ where ___ test ___' And also an array like this const blankData = ['there', 'you', 'it'] my expected result is hello there where you test it wha... More on stackoverflow.com
🌐 stackoverflow.com
July 18, 2021
Top answer
1 of 2
1

I am creating an object letters which will get all the letters as the key and the value is an array with all the index of the letter in the word.

eg: The word hey will have the object like

{
    "h":[0],
    "e":[1],
    "y":[2]
}

I am using Array#map() to create an array of stars.

To know where to put the letter instead of a star I am using Array#find() to find the key with the letter typed. If the result is undefined - it means that the letter you tried is not in the word. If it is defined, I replace the star at the index of the letter with the correct letter

$(document).ready(function() {

  $('#div_letter').hide();
  var word_input = $('#word');
  var word_array = [];
  var letters = {};

  word_input.keypress(function(e) {
    if (e.keyCode == 13) {
      word_array = Array.from(word_input.val());
      letters = word_array.reduce((obj,letter, i)=>{
        obj[letter] = obj[letter] || [];
        obj[letter].push(i);
        return obj;
      }, {});
      var stars = word_array.map(l=>"*");
      
      $('#div_word').hide();
      $('#div_letter').show();
      $('#display').text(stars.join(""));

      $('#letter').keypress(function(e) { //if any key is pressed
        var _letter = $(this);
        var char = String.fromCharCode(e.which).toLowerCase(); //get which key
        
        let keyLetter = Object.keys(letters).find(l=>l.toLowerCase()===char);
        if(keyLetter){
          letters[keyLetter].forEach(i=>stars[i] = char);
          $('#display').text(stars.join(""));
        }
        _letter.val("");
      });
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<div id="div_word">
  <label>Enter your Word</label>
  <input id="word" type="text"><br>
</div>

<div id="div_letter">
  <label>Type a letter</label>
  <input id="letter" type="text" /><br>
</div>

<label>Display Array:</label>
<div id="display">

</div>

2 of 2
1

I just turned the word into a dictionary, then used that to determine where in the mask to replace a character.

I have purposely left out the user interaction code as that is surplus to the requirements.

See working plnkr

var word = "milly";

var mask = '*'.repeat(word.length);

var dict = word
            .split('')
            .map((m, i) => ({ letter: m, index: i }))
            .reduce((f, c) => {
              if (f[c.letter]) {
                f[c.letter] = [f[c.letter], c.index];
                return f;
              }

              return Object.assign({}, f, {[c.letter]: c.index})
            }, {});

console.log(dict); // Object {m: 0, i: 1, l: Array[2], y: 4}

var input = "l";

function revealLetter(input, mask, dict) {

  var ind = dict[input];

  if (ind !== undefined && ind instanceof Array) {
    mask = ind.reduce((f, i) => changeCharacter(f, i, input), mask);
  } else {
    mask = changeCharacter(mask, ind, input);
  }

  return mask;
}

function changeCharacter(mask, ind, input) {
  return mask.split('').map((s, i, o) => i === ind ? input : o[i]).join('');
}

mask = revealLetter(input, mask, dict);

console.log(mask); // **ll*

Just used a few map reduces. You know when you need to use reduce, because you'll have an array and you'll want a single value back.

If there is anything you want explicitly clarifying just say - but the logs easily show what happens at each stage.

🌐
freeCodeCamp
forum.freecodecamp.org › javascript
How to replace a letter in an array? - JavaScript - The freeCodeCamp Forum
March 10, 2023 - Please help me how to change the letter in the array without creating a new array ?
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript - MDN Web Docs
However, this replacer would be hard to generalize if we want it to work with any regex pattern. The replacer is variadic — the number of arguments it receives depends on the number of capturing groups present. We can use rest parameters, but it would also collect offset, string, etc. into the array.
🌐
Programiz
programiz.com › javascript › examples › replace-character-string
JavaScript Program to Replace all Instances of a Character in a String
Javascript Array join() // program ... console.log(result); Output · LeArning JAvAScript ProgrAm · In the above example, the RegEx is used with the replace() method to replace all the instances of a character in a string....
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 44197991 › replace-character-of-a-string-with-the-character-from-an-array
javascript - Replace character of a string with the character from an array - Stack Overflow
If I understand correctly, you want to change a string like "test" into "🇹🇪🇸🇹", is that correct? ... Save this answer. ... Show activity on this post. This is all about the coding of characters. Each of your cool-looking letters has length 2. So when you try to get such character directly by index, you receive just a half of it.
🌐
TutorialsPoint
tutorialspoint.com › article › How-do-I-replace-a-character-at-a-particular-index-in-JavaScript
How do I replace a character at a particular index in JavaScript?
July 20, 2022 - let charArray = [...string]; // convert string to array charArray[index] = newCharacter; // replace character string = charArray.join(''); // join array back to string
🌐
Stack Overflow
stackoverflow.com › questions › 33920922 › find-charater-in-array-and-replace-this-with-new-values
javascript - Find charater in array and replace this with new values - Stack Overflow
rows = [2,5,50,'55-60',74,'80-84']; var newRows=new Array(); for (var i in rows) { if (matches = rows[i].toString().match(/^(\d+)\-(\d+)$/)) //use regexp to check ranges { for (var j=parseInt(matches[1]);j<=parseInt(matches[2]);j++) newRows.push(j); }else{ newRows.push(rows[i]); } }
🌐
W3Schools
w3schools.com › jsref › jsref_replace.asp
JavaScript String replace() Method
The replace() method searches a string for a value or a regular expression.