Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I remove characters from a string given in an array

for self development purposes I want to create a function with two parameters – string and an array. It should return a string without the letters given in the array.

function filterLetters(str, lettersToRemove) {

}

const str = filterLetters('Achievement unlocked', ['a', 'e']);

Could someone give me any pointers on how to achieve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

For each letter to be replaced, replace it. When done, return the updated string:

function filterLetters(str, lettersToRemove) {
    lettersToRemove.forEach(function(letter){
        str = str.replaceAll(letter, '');
    })
    return str
}

Also see How to replace all occurrences of a string in JavaScript.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading