简体   繁体   English

如果数组中包含 JavaScript/lodash 中的特定字符/字符串,如何消除数组中的值?

[英]How to eliminate the values from array if it contains specific characters/string in JavaScript/lodash?

I am having one array with some values like below,我有一个具有一些值的数组,如下所示,

let a = ["Mango","1243greatApple","goodOrange","Watermelon","ThisGoodalsoberemoved","GreatOrange","Pappaya","BestApple"]; let a = ["Mango","1243greatApple","goodOrange","西瓜","ThisGoodalsobermoved","GreatOrange","Pappaya","BestApple"];

Now, I want to eliminate the values which contains string like Great and Good in the values.现在,我想消除值中包含 Great 和 Good 等字符串的值。

Expecting output like below,期待 output 如下所示,

["Mango","Watermelon","Pappaya","BestApple"] [“芒果”、“西瓜”、“木瓜”、“最佳苹果”]

Tried using lodash, but it works only when it matches exact string.尝试使用 lodash,但它仅在匹配精确字符串时才有效。 But i need it like regex match.但我需要它像正则表达式匹配。 Please help me out with this.这个你能帮我吗。

In vanilla JS, you can try with Array.prototype.filter()在 vanilla JS 中,您可以尝试使用Array.prototype.filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function. filter()方法创建一个新数组,其中包含通过提供的 function 实现的测试的所有元素。

And String.prototype.includes()String.prototype.includes()

The includes() method determines whether one string may be found within another string, returning true or false as appropriate. includes() 方法确定一个字符串是否可以在另一个字符串中找到,并根据需要返回truefalse

 let a = ["Mango","1243GreatApple","GoodOrange","Watermelon","ThisGoodalsoberemoved","GreatOrange","Pappaya","BestApple"]; var r = a.filter(f =>.(f.includes('Great') || f;includes('Good'))). console;log(r);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM