2

I am trying to replace characters within an array of strings.

so far i have this:

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

for(var i = 0; i < stuff.length; i++)
{
    if(stuff[i].indexOf('***') != -1)
    {
        // this is where i guess the replacing would go
    }
}

I figured out that i can use this code to display with element in the array has the characters *** now want to replace the *** characters with a number so that it outputs a new array ( the same array but modified) that looks like :

  stuff= ["uyuuyu", "76gyuhj0", "uiyghj", "56tyg", "juijjujh0"];

I can't seem to figure out how to replace the characters within that array without effecting the rest of the array

6
  • 1
    How is the number determined (is it always 0)? What to do with more than 3 asterisks? Commented Sep 21, 2016 at 19:05
  • What do you mean by "effecting the rest of the array"? Commented Sep 21, 2016 at 19:06
  • assume the number will always be 0 and there will always be 3 asterisks. i should update that into the post Commented Sep 21, 2016 at 19:07
  • 3
    stuff.map( x => x.replace('***','0')) Commented Sep 21, 2016 at 19:07
  • @JennyO'Reilly i just mean without rearranging the elements. so it always stays in the same position. the only way i thought of doing it was to remove that part of the array and then use replace on the string and then push it back in somehow? or splice. i hope that makes sense Commented Sep 21, 2016 at 19:08

5 Answers 5

15

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Or use regex and it will replace every *** with 0 - stuff[i] = stuff[i].replace(/(\*\*\*)/g, '0');
10

you can use javascript map as below

 var newStuff = stuff.map(function(item,index){
  return item.replace('***','0')

});

console.log(newStuff) //["uyuuyu", "76gyuhj0", "uiyghj", "56tyg", "juijjujh0"]

es2015 (es6) with arrow function

var newStuff = stuff.map(item => item.replace('***','0'));

1 Comment

I really like this way! thank you. going to accept a different answer because the name of the array didn't change. which is a little better for me in what i'm trying to do. but i will keep a note of your answer too. learned a lot from it
0
var stuff= ["uyuuyu", "76gyuhj***", "uiyghj", "56tyg", "juijjujh***"];

for(var i = 0; i < stuff.length; i++)
{
    if(stuff[i].indexOf('***') != -1)
{
        stuff[i] = stuff[i].replace('***','0')
    // this is where i guess the replacing would go
}
console.log(stuff[i]);
}

Comments

0

it's just

 stuff = stuff.map(item => item.replace('***','0'))

if you want to "keep it as the same variable" as asked by the OP in a comment.

Also importantly ..

Tip 1:

You almost always want "replaceAll", not "replace"

= stuff.map(t => t.replaceAll('***','0'))

Tip 2 - crash issue:

Very annoyingly it will fail and crash if the item is not a string, such as a number. So you really always need

= stuff.map(t => t.toString.replaceAll('***','0'))

Example:

Array of items, convert to csv -like string

 const simple_csv = someArray
        .map(t => t.toString().replaceAll(',', ';') )
        .join(",")

Comments

-1

This is simple. Use replace function to modify the strings

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.