使用其他数组的元素.filter()数组 [英] .filter() array using another array's elements
问题描述
我的人的名字与他们的语文知识沿着阵列。我想要做的就是通过一个过滤器上的语言列,并过滤掉不符合任何结果。
这是样品阵列
myarray的无功= [[Steppen,西班牙波兰],
[狼,西班牙波兰菲律宾语],
[阿曼达,西班牙]
[阿达,波兰],
[朗达,西班牙他加禄语]];
至于传递过滤器,它可以是一种语言或多个。即使从过滤器一种语言相匹配 - 结果应该返回。因此,例如,他加禄语的过滤器应该返回 - 狼和朗达。 西班牙波兰的过滤器应该回报大家 - 有无论是在西班牙或波兰的比赛
我写的过滤功能,但由于某种原因,它被卡住,当我通过过滤器菲律宾语只迭代到第二单元阵列(西班牙波兰菲律宾语),并重演多次,而不是前进。
我在做什么错了,我应该是迭代不同?
VAR userPassedFilter =新的Array();
userPassedFilter [0] =他加禄语; newArray = consolidatedFilters(myarray的,userPassedFilter);
的console.log(newArray); 功能consolidatedFilters(passedArray,passedFilter)
{
VAR filteredArray = passedArray.filter(
功能(EL)
{
对于(VAR I = 0; I< passedArray.length;我++)
{
的console.log(我是+ I);
对(在passedFilter变种j)条
{
(顺利通过过滤器J+ passedFilter [J])的console.log;
的console.log(传递的数组我+ passedArray [I] [1]);
的console.log(字符串搜索+ passedArray [I] [1] .search(passedFilter [J])); 如果(passedArray [I] [1] .search(passedFilter [j]的)!= - 1)
{
返回true;
}
}
}
返回false;
}
);
返回filteredArray;
}
对我来说,好像你正在做的有点太复杂了。
- 遍历三次(
过滤器
,为
循环,在
循环)。 - 使用了数组的
在
循环。 - 同时使用
新阵列
和[...]
。
我更新了这一点,它看起来这是你想要什么: http://jsfiddle.net/pimvdb/RQ6an/
myarray的无功= [[Steppen,西班牙波兰],
[狼,西班牙波兰菲律宾语],
[阿曼达,西班牙]
[阿达,波兰],
[朗达,西班牙他加禄语]];VAR userPassedFilter = [菲律宾语];newArray = consolidatedFilters(myarray的,userPassedFilter);
的console.log(newArray);功能consolidatedFilters(passedArray,passedFilter){
VAR filteredArray = passedArray.filter(
功能(EL){//对每个人执行
对于(VAR I = 0; I< passedFilter.length;我++){//对滤波器迭代
如果(EL [1] .indexOf(passedFilter [I])!= - 1){
返回true; //如果这个人知道这种语言
}
}
返回false;
}
);
返回filteredArray;
}
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.
This is the sample array
var myArray = [["Steppen", "Spanish Polish"],
["Wolf", "Spanish Polish Tagalog"],
["Amanda", "Spanish"],
["Ada", "Polish"],
["Rhonda", "Spanish Tagalog"]];
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.
- Iterating three times (
filter
,for
loop,for in
loop). - Using a
for in
loop for an array. - Using both
new Array
and[...]
.
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屋!