我将在 lodash 流中添加 2 个函数:
function normalizedFormFields(fields) { // needs only 1 argument
return _.mapValues( fields, function( value ) {
return { 'content': value };
} );
}
function mergedModelAndFormFieldss(model, normalizedFormFields) {
return _.merge( {}, model, normalizedFormFields )
}
const execution = _.flow( normalizedFormFields, mergedModelAndFormFieldss )
const errorMessageBag = function( fields, model ) {
return execution( fields, model ) // initiate flow with 2 arguments
}
如您所见,第一个函数 normalizedFormFields 接受一个参数。第二个需要2:前一个函数返回的值(这是流的正常行为),另一个需要:模型。
但在 errorMessageBag 调用中,我使用 2 个参数启动流程。除了第一个函数的返回产物之外,如何让第二个参数可用于第二个函数? 如您所见,问题在于流程中的第一个函数接受并且只需要一个参数。这是一种“ curry ”应该发挥作用的情况吗?请说明。
最佳答案
试试这个,应该有效:
function normalizedFormFields(fields) {
return _.mapValues( fields, function( value ) {
return { 'content': value };
});
}
function mergedModelAndFormFieldss(model, normalizedFormFields) {
return _.merge( {}, model, normalizedFormFields )
}
const errorMessageBag = function( fields, model ) {
return _.flow(
normalizedFormFields,
mergedModelAndFormFieldss.bind(this, model)
)(fields)
}
关于javascript - lodash 流和多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304967/