【发布时间】:2021-05-21 03:54:13
【问题描述】:
作为一个在函数式编程中应用lodash库的初学者,我发现._flow并没有执行放入的函数。我的问题是如何在流中应用map、reduce或filter?还是我做错了什么?非常感谢任何帮助。
例如:
const _=require('lodash');
const scores = [50, 6, 100, 0, 10, 75, 8, 60, 90, 80, 0, 30, 110];
const newArray=_.filter(scores,val=>val<=100);
const newArray2=_.filter(newArray,val=>val>0);
console.log(newArray2);
// the output is
/*[
50, 6, 100, 10, 75,
8, 60, 90, 80, 30
]*/
但是,当我将它设为两个独立的函数并放入流时,它什么也没做。
const newRmvOverScores=_.filter(val=>val<=100);
const newRmvZeroScores=_.filter(val=>val>0);
const aboveZeroLess100=_.flow(newRmvOverScores,newRmvZeroScores)(scores);
console.log(aboveZeroLess100);
// the output is:
/*[
50, 6, 100, 0, 10, 75,
8, 60, 90, 80, 0, 30,
110
]*/
我找到的几个参考资料:
[1]Using lodash, why is the map method within flow not working? [2]https://lodash.com/docs/4.17.15#flow
【问题讨论】:
标签: javascript functional-programming lodash