Javascript 比较并从数组列表中排除字符串关键字

Javascript 比较并从数组列表中排除字符串关键字,javascript,arrays,string,arraylist,match,Javascript,Arrays,String,Arraylist,Match,我有一组字符串关键字和一组数组列表,用于比较和排除最终字符串显示中的项目。我插入了一个拆分以隔离每个字符串 代码如下: var keywords = "Did the cow jump over the moon?"; var keywords_parsed = keywords.split(" "); var exclusionlist = ["a","is","of","in","on","it","to","if","so","the","i","we","did"]; 最后的字符串显示

我有一组字符串关键字和一组数组列表,用于比较和排除最终字符串显示中的项目。我插入了一个拆分以隔离每个字符串

代码如下:

var keywords = "Did the cow jump over the moon?";
var keywords_parsed = keywords.split(" ");
var exclusionlist = ["a","is","of","in","on","it","to","if","so","the","i","we","did"];
最后的字符串显示应该是

cow, jump, over, moon
代码应排除“Did”、“The”和其他“The”


我怎样才能做到这一点呢?

这里有一条单行线,只是为了好玩:

keywords.match(/\w+/g)
        .map(function(i) {return i.toLowerCase();})
        .filter(function(i) { return exclusionlist.indexOf(i) == -1; })
jsFIDLE:

该方法就是您要寻找的。你可以这样做来实现你想要的

var keywords = "Did the cow jump over the moon?";

// change lowercase then split the words into array
var keywords_parsed = keywords.toLowerCase().split(" ");

var exclusionlist = ["a","is","of","in","on","it","to","if","so","the","i","we","did"];

// now filter out the exclusionlist
keywords_parsed.filter(function(x) { return exclusionlist.indexOf(x) < 0 });
var keywords=“奶牛跳过月亮了吗?”;
//更改小写,然后将单词拆分为数组
var keywords_parsed=keywords.toLowerCase().split(“”);
var ExclutionList=[“a”、“is”、“of”、“in”、“on”、“it”、“to”、“if”、“so”、“the”、“i”、“we”、“did”];
//现在过滤掉排除列表
关键词_parsed.filter(函数(x){返回排除列表.indexOf(x)<0});

你的问题是什么?换句话说,您尝试了哪些不起作用的方法?我需要使用ExclutionList(数组)作为要从keywords变量中排除的所有字符串的主列表。因此,如果关键字不同,则排除列表将用作要显示的不必要项目的过滤器,即cow、jump、over、moonYes,但这是一个代码请求,而不是编程问题。你应该先用谷歌搜索一些东西,然后告诉我们你已经尝试过的东西,这样我们就可以看到问题所在,改进你现有的代码,而不是为你编写代码:这将帮助你更好地记住它。这在另一种方法上也起到了作用,谢谢@zerkms。