.filter() 数组使用另一个数组的元素 [英] .filter() array using another array's elements
问题描述
我有一系列人的名字以及他们的语言知识.我想要做的是将过滤器传递到语言列并过滤掉任何不匹配的结果.
I have an array of people's names along with their knowledge of languages. What I want to do is pass a filter onto the language column and filter out any results that don't match.
这是样本数组
var myArray = [["Steppen", "Spanish Polish"],
["Wolf", "Spanish Polish Tagalog"],
["Amanda", "Spanish"],
["Ada", "Polish"],
["Rhonda", "Spanish Tagalog"]];
就传递过滤器而言,它可以是一种语言或多种语言.即使过滤器中的一种语言匹配 - 也应该返回结果.因此,例如,应返回他加禄语"过滤器 - Wolf 和 Rhonda.西班牙语波兰语"过滤器应该返回每个人 - 有西班牙语或波兰语的匹配项.
As far as passing in filters, it could be either one language or many. Even if one language from a filter matches - the result should be returned. So for example, a filter of "Tagalog" should return - Wolf and Rhonda. A filter of "Spanish Polish" should return everyone - there's either a match in Spanish or Polish.
我编写了过滤器函数,但由于某种原因它卡住了,当我通过过滤器他加禄语"时,它只会迭代到数组中的第二个单元格(西班牙波兰他加禄语)并重复多次而不是前进.
I wrote the filter function but for some reason it's getting stuck, when I pass the filter "Tagalog" it only iterates to the second cell in the array (Spanish Polish Tagalog) and repeats itself multiple times instead of going forward.
我做错了什么,我应该以不同的方式迭代吗?
What am I doing wrong, should I be iterating differently?
var userPassedFilter = new Array();
userPassedFilter[0] = "Tagalog";
newArray = consolidatedFilters(myArray, userPassedFilter);
console.log(newArray);
function consolidatedFilters(passedArray, passedFilter)
{
var filteredArray = passedArray.filter(
function(el)
{
for (var i = 0; i < passedArray.length; i++)
{
console.log("i is " + i);
for (var j in passedFilter)
{
console.log("Passed Filter j " + passedFilter[j]);
console.log("Passed Array i " + passedArray[i][1]);
console.log("String Search " + passedArray[i][1].search(passedFilter[j]));
if (passedArray[i][1].search(passedFilter[j]) != -1)
{
return true;
}
}
}
return false;
}
);
return filteredArray;
}
推荐答案
在我看来,你让它变得有点太复杂了.
To me it seems like you're making it a little too complicated.
- 迭代 3 次(
filter
、for
循环、for in
循环). - 对数组使用
for in
循环. - 同时使用
new Array
和[...]
.
- Iterating three times (
filter
,for
loop,for in
loop). - Using a
for in
loop for an array. - Using both
new Array
and[...]
.
我更新了一点,看起来这就是你想要的:http://jsfiddle.net/pimvdb/RQ6an/.
I updated it a little and it looks like this is what you want: http://jsfiddle.net/pimvdb/RQ6an/.
var myArray = [["Steppen", "Spanish Polish"],
["Wolf", "Spanish Polish Tagalog"],
["Amanda", "Spanish"],
["Ada", "Polish"],
["Rhonda", "Spanish Tagalog"]];
var userPassedFilter = ["Tagalog"];
newArray = consolidatedFilters(myArray, userPassedFilter);
console.log(newArray);
function consolidatedFilters(passedArray, passedFilter) {
var filteredArray = passedArray.filter(
function(el) { // executed for each person
for (var i = 0; i < passedFilter.length; i++) { // iterate over filter
if (el[1].indexOf(passedFilter[i]) != -1) {
return true; // if this person knows this language
}
}
return false;
}
);
return filteredArray;
}
这篇关于.filter() 数组使用另一个数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!