2

I am trying to remove punctuation from each string within an array, but this problem would exist for trying to delete any type of character within strings within an array.

I have attempted to create 3 loops: The first loop iterates over each item in arrayA that I'm aiming to edit. The second loop iterates through each character in each string in arrayA. The third loop checks whether the character in arrayA matches any character in arrayB, and deletes it if it does.

Nothing is being deleted however, and I'm not sure why.

This is my code so far:

let arrayA = ['abc', 'def', 'ghi'];
let arrayB = ['a', 'e', 'i', 'o', 'u'];

arrayA.forEach((item) => {
    for (let i=0; i < item.length; i++) {
        for (let arrayBIndex = 0; arrayBIndex < arrayB.length; arrayBIndex++) {
            item.replace(arrayB[arrayBIndex], '');
        };
    };
});
console.log(arrayA);

I have searched for other questions dealing with this, but I haven't been able to find any answers, specifically where the elements to delete are contained in another list. Thank you for your help.

1
  • 1
    String.replace does not modify the original string. You should use the return value of the function instead. Commented Dec 24, 2017 at 11:37

4 Answers 4

3

You can generate regular expression using arrayB and then using array#map iterate through each word in arrayA and use string#replace to get rid of words from arrayB.

let arrayA = ['abc', 'def', 'ghi'],
    arrayB = ['a', 'e', 'i', 'o', 'u'],
    regExp = new RegExp(arrayB.join('|'), 'g'),
    result = arrayA.map(word => word.replace(regExp, ''));
    
console.log(result);

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

1 Comment

Thanks for your answer, it helped a lot. Just as a clarification for anyone using this with punctuation specifically. I discovered that punctuation as strings cannot necessarily be indicated using just apostrophes in this solution, as the fullstop ('.') matches any character. For removing punctuation, arrayB = [',', '\\.', '"', '\'', '!', '-'] as this prevents the fullstop and the apostrophe from behaving unexpectedly with the rest of the code.
1

Use Array.prototype.splice(), take a look on this:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Comments

1

If you wish to follow with arrays, I would suggest to transform your strings into an array of characters and using array filter operator.

However you can probably achieve what you want to do with regular expressions

    const arrayA = ['abc', 'def', 'ghi'];
    const arrayB = ['a', 'e', 'i', 'o', 'u'];
    const result = arrayA
                      .map(s => [...s]) // array of chars
                      .map(chars => chars.filter(ch=>!arrayB.includes(ch)).join(''))//filter out invalid char and transform back into string

console.log(result)

2 Comments

arrayA.map(item => [...item].filter(char => !arrayB.includes(char)).join("")) <- thats less ugly
that is just a matter of preference. Actually putting the "join" (from the snippet of my answer) in its own map would be even better in my opinion (easier to see the "algorithm" -> map(toChar).filter(invalidChar).map(toString)
-1
 const result = arrayA.map(item => {
   let replaced = "";
   for(const char of item)
     if(!arrayB.includes(char)) 
        replaced += char;
   return replaced;
});

Strings are immutable. Every mutation returns a new string instead of mutating the original.

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.