javascript 按字母过滤数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32819434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Filter array by letter
提问by MateBoy
Pardon me if this has been asked but I searched and didn't find the specific implementation of my problem.
如果有人问过这个问题,请原谅我,但我搜索了但没有找到我的问题的具体实现。
Anyway, I'm currently learning high-order functions in JavaScript and I'm at the array.prototype.filter function. I understand its purpose (as its name so conveniently implies) but I'm having trouble implementing this:
无论如何,我目前正在学习 JavaScript 中的高阶函数,并且我在使用 array.prototype.filter 函数。我理解它的目的(正如它的名字所暗示的那样),但我在实现这一点时遇到了麻烦:
So, say I have an array of names, like this:
所以,假设我有一个名称数组,如下所示:
var names = ["Anna", "Bob", "Charles", "Daniel",
"Allison", "Beatrice", "Cindy", "Fiona"];
And then I want to, say, filter that array by all entries that start with the letter "A". I'm aware of the fact that I could do this:
然后我想通过所有以字母“A”开头的条目过滤该数组。我知道我可以这样做:
var filteredNames = names.filter(function(word) {
return word[0] === "A";
});
And that would work just fine. But say I want to be less explicit and make it more adaptable to more situations. Say I want to program the filtering so that I can say "return only the entries that have the letter "x" at index [y]", for example "return only the entries that have the letter "F" at index[3].
这将工作得很好。但是说我想不那么明确,让它更能适应更多的情况。假设我想对过滤进行编程,以便我可以说“仅返回在索引 [y] 处具有字母“x”的条目,例如“仅返回在索引 [3] 处具有字母“F”的条目.
How can I achieve that?
我怎样才能做到这一点?
采纳答案by PMerlet
You can define your own filter function :
您可以定义自己的过滤功能:
function filter(names, index, letter) {
var filteredNames = names.filter(function(word) {
return word.charAt(index) === letter;
});
return filteredNames;
}
回答by axelduch
A regexp will be more flexible I guess
我猜正则表达式会更灵活
var filteredNames = names.filter(function(word) {
return /^A/.test(word);
});
A generic way to use it
使用它的通用方法
function filterMatches(words, regexp) {
return words.filter(function (word) {
return regexp.test(word);
});
}
filterMatches(words, /^A/); // for letter A, index 0
filterMatches(words, /^.{3}B/); // for letter B, index 4
回答by Cedric
why not create a function to do just what you want? From your code it be like this:
为什么不创建一个函数来做你想做的事?从你的代码来看,它是这样的:
function filterName(arrayOfNames, index, letter) {
var filteredNames = arrayOfNames.filter(function(word) {
return word[index] === letter;
});
return filteredNames
}
So you can just pass on the array, index and letter to it:
因此,您可以将数组、索引和字母传递给它:
console.log(filterName(arrayOfNames, 3, 'x'));
回答by TheVigilant
You can create your own custom function
您可以创建自己的自定义函数
var array =["Anna", "Bob", "Charles", "Daniel", "Allison", "Beatrice", "Cindy", "Fiona"];
var matched_terms = [];
var search_term = "an";
search_term = search_term.toLowerCase();
array.forEach(item => {
if(item.toLowerCase().indexOf(search_term) !== -1 ){
console.log(item);
matched_terms.push( item );
}
console.log(matched_terms);
})
回答by Emil Vikstr?m
Your filter function can be however complex you want, as long asit returns true
for values to keep and false
for values to skip. For example:
您的过滤器函数可以是您想要的复杂程度,只要它返回true
值以保留false
值和跳过值即可。例如:
var filteredNames = names.filter(function(word) {
return word.length > 3 && word[3] === "Y";
});
Even better, you can create the callback function dynamically since functions are first-class citizensin JS:
更好的是,您可以动态创建回调函数,因为函数是JS 中的一等公民:
function createCallback(position, letter) {
return function (word) {
return word.length > position && word[position] === letter;
}
}
var filteredNames = names.filter(createCallback(3, "Y"));
Higher-order functions like filter
and map
takes other functions as values which help them do their work. You can make this an even deeper redirection now by building an array of functions and them apply them all using the higher-order function reduce:
高阶函数喜欢filter
并map
接受其他函数作为帮助它们完成工作的值。您现在可以通过构建一个函数数组并使用高阶函数reduce将它们全部应用,从而使其成为更深层次的重定向:
function createCallback(position, letter) {
return function (word) {
return word.length > position && word[position] === letter;
}
}
function applyFilter(currentFilter, currentNames) {
return currentNames.filter(currentFilter);
}
var filters = [
createCallback(3, "Y"),
createCallback(0, "A"),
createCallback(2, "Y")
];
var ultraFilteredNames = filters.reduce(applyFilter, names);