Javascript:如何删除数组中字符串中的特定字符值

时间:2022-12-29 21:17:35

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.

我尝试创建3个循环:第一个循环遍历arrayA中要编辑的每个项。第二个循环遍历arrayA中的每个字符串中的每个字符。第三个循环检查arrayA中的字符是否与arrayB中的任何字符匹配,如果匹配,则删除该字符。

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.

我已经搜索了其他与此相关的问题,但是我没有找到任何答案,特别是要删除的元素包含在另一个列表中。谢谢你的帮助。

4 个解决方案

#1


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.

您可以使用arrayB生成正则表达式,然后使用数组#map遍历arrayA中的每个单词,并使用字符串#replace从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);

#2


1  

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

使用Array.prototype.splice(),看看这个:

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

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

#3


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)

#4


-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.

字符串是不可变的。每个突变都返回一个新的字符串,而不是原来的。

#1


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.

您可以使用arrayB生成正则表达式,然后使用数组#map遍历arrayA中的每个单词,并使用字符串#replace从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);

#2


1  

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

使用Array.prototype.splice(),看看这个:

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

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

#3


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)

#4


-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.

字符串是不可变的。每个突变都返回一个新的字符串,而不是原来的。