具有条件输出/应用的Javascript数组过滤器
我想知道,在JavaScript中是否有一种方法可以过滤数组或流,并将函数a应用于所有匹配项,将函数B应用于所有非匹配项。下面是一些示例代码,对其进行了详细解释:具有条件输出/应用的Javascript数组过滤器,javascript,arrays,filter,conditional-statements,Javascript,Arrays,Filter,Conditional Statements,我想知道,在JavaScript中是否有一种方法可以过滤数组或流,并将函数a应用于所有匹配项,将函数B应用于所有非匹配项。下面是一些示例代码,对其进行了详细解释: // initial data var names = ['Matthias', 'Maria', 'Bob', 'Anton']; var namesWithM; var namesWithoutM; // gets only names starting with M, but not the others
// initial data
var names = ['Matthias', 'Maria', 'Bob', 'Anton'];
var namesWithM;
var namesWithoutM;
// gets only names starting with M, but not the others at the same time
namesWithM = names.filter(name => name.startsWith('M'))
// conditional lambda version
namesWithM = [];
namesWithoutM = [];
names.forEach(name => name.startsWith('M') ? namesWithM.push(name) : namesWithoutM.push(name));
// conditional classical version
namesWithM = [];
namesWithoutM = [];
names.forEach(function(name) {
if (name.startsWith('M'))
namesWithM.push(name)
else
namesWithoutM.push(name);
});
最早的版本只处理匹配项,但使用filter
,而不是forEach
。是否有任何方法可以使用过滤器
,同时对匹配项和非匹配项应用函数?类似于以下伪代码:
names.filter(name.startsWith('M')).apply(namesWithM::push).or(namesWithoutM::push);
const name=['Matthias','Maria','Bob','Anton'];
职能A(项目){
console.log('filtered');
控制台日志(项目);
}
职能B(项目1){
console.log('not-ffiltered');
控制台日志(项目);
}
常量filteredNames=names.filter(name=>{
const isValid=name.startsWith('M')
如果(有效)
A(姓名)
其他的
B(姓名)
返回有效;
})
const name=['Matthias','Maria','Bob','Anton'];
职能A(项目){
console.log('filtered');
控制台日志(项目);
}
职能B(项目1){
console.log('not-ffiltered');
控制台日志(项目);
}
常量filteredNames=names.filter(name=>{
const isValid=name.startsWith('M')
如果(有效)
A(姓名)
其他的
B(姓名)
返回有效;
})
我将使用reduce
将数据分组为上述两种情况。我看不出有任何理由在这里使用filter
let names=['Matthias','Maria','Bob','Anton'];
让[namesWithM,namesWithoutM]=名称。减少((acc,name)=>{
if(name.startsWith('M')){
acc[0]=[…(acc[0]| |[]),名称]
返回acc;
}
acc[1]=[…(acc[1]| |[]),名称]
返回acc;
}, [])
//更简单的版本
console.log(namesWithM,namesWithoutM);
让[namesWithM1,namesWithoutM1]=names.reduce((acc,name)=>{
常量索引=编号(!name.startsWith('M');
acc[索引]=[…(acc[索引]| |[]),名称];
返回acc;
}, [])
console.log(namesWithM1,namesWithoutM1)
我将使用reduce
将数据分组为上述两种情况。我看不出有任何理由在这里使用filter
let names=['Matthias','Maria','Bob','Anton'];
让[namesWithM,namesWithoutM]=名称。减少((acc,name)=>{
if(name.startsWith('M')){
acc[0]=[…(acc[0]| |[]),名称]
返回acc;
}
acc[1]=[…(acc[1]| |[]),名称]
返回acc;
}, [])
//更简单的版本
console.log(namesWithM,namesWithoutM);
让[namesWithM1,namesWithoutM1]=names.reduce((acc,name)=>{
常量索引=编号(!name.startsWith('M');
acc[索引]=[…(acc[索引]| |[]),名称];
返回acc;
}, [])
console.log(namesWithM1,namesWithoutM1)代码>过滤器返回一个数组。因此,您可以使用此数组填充name
,它可以以M开头,也可以不以M开头
在下面的示例中,过滤器使用名称以M
开头填充数组。在filter
callback中,不以M开头的名称填充到另一个数组中
//初始数据
变量名称=['Matthias','Maria','Bob','Anton'];
var-m;
var namesWithoutM=[];
namesWithM=names.filter((名称)=>{
如果(!name.startsWith('M')){
没有M.push的名称(名称)
}
返回name.startsWith('M');
});
console.log(namesWithM,namesWithoutM)代码>过滤器返回一个数组。因此,您可以使用此数组填充name
,它可以以M开头,也可以不以M开头
在下面的示例中,过滤器使用名称以M
开头填充数组。在filter
callback中,不以M开头的名称填充到另一个数组中
//初始数据
变量名称=['Matthias','Maria','Bob','Anton'];
var-m;
var namesWithoutM=[];
namesWithM=names.filter((名称)=>{
如果(!name.startsWith('M')){
没有M.push的名称(名称)
}
返回name.startsWith('M');
});
console.log(namesWithM,namesWithoutM)代码>正确,但代码又相当长。但是reduce和累加器的好例子。在我的版本中,我使用了条件lambda代码,但使用reduce而不是filter。我不能再压缩它了。我发现这个很好的解释,关于如何去做,多少和我的问题是一样的。是的,但这又是一个很长的代码。但是reduce和累加器的好例子。在我的版本中,我使用了条件lambda代码,但使用reduce而不是filter。我不能再压缩它了,因为我发现这个很好的解释,关于如何去做,多少和我的问题是一样的。