在 lodash 中有条件地向 _.chain() 添加方法?
Conditionally add methods to _.chain() in lodash?
例如,在此示例中,是否可以有条件地在此链中添加 .upperCase()
方法?
const isUpperCase = true;
const users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
const youngest = _.chain(users)
.sortBy('age')
.map(function(o) {
return o.user + ' is ' + o.age;
})
.head()
// Only add this method if isUpperCase above is set to true
.upperCase()
.value();
使用链接时有以下三种方法:
有条件地附加到链
您必须保存部分链,然后有条件地对其调用所需的方法并保存输出。然后你可以继续正常处理:
const isUpperCase = true;
const users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
let chain = _.chain(users)
.sortBy('age')
.map(function(o) {
return o.user + ' is ' + o.age;
})
.head();
// Only add this method if isUpperCase above is set to true
if (isUpperCase) {
chain = chain.upperCase();
}
const youngest = chain.value();
console.log(youngest);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
使用 .thru()
添加手动步骤
.thru()
方法将通过自定义函数传递链的值和 return 结果。这允许您在自定义函数内应用条件处理。
const isUpperCase = true;
const users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
const youngest = _.chain(users)
.sortBy('age')
.map(function(o) {
return o.user + ' is ' + o.age;
})
.head()
.thru(function(o) {
if (isUpperCase)
return _.upperCase(o);
return o;
})
.value();
console.log(youngest);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
有条件地处理链的结果
这可能更容易,因为您不需要篡改链本身。根据条件,有值后可以申请upperCase
const isUpperCase = true;
const users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
const result = _.chain(users)
.sortBy('age')
.map(function(o) {
return o.user + ' is ' + o.age;
})
.head()
.value();
let youngest = result;
// Only add this method if isUpperCase above is set to true
if (isUpperCase){
youngest = _.upperCase(result);
}
console.log(youngest);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
使用 flow
的功能组合
还有一种替代方法,即使用 functional composition to set up the processing chain. Then you can add or alter parts of this easily and then finally execute it against any dataset. The Lodash FP 分布提供非常适合此任务的现成函数。所有函数都被柯里化,数据参数移到最后。
const { flow, sortBy, map, head, upperCase } = _;
const isUpperCase = true;
const users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
const chain = [
sortBy('age'),
map(function(o) {
return o.user + ' is ' + o.age;
}),
head
];
// Only add this method if isUpperCase above is set to true
if (isUpperCase) {
chain.push(upperCase);
}
const youngest = flow(...chain)(users)
console.log(youngest);
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>
例如,在此示例中,是否可以有条件地在此链中添加 .upperCase()
方法?
const isUpperCase = true;
const users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
const youngest = _.chain(users)
.sortBy('age')
.map(function(o) {
return o.user + ' is ' + o.age;
})
.head()
// Only add this method if isUpperCase above is set to true
.upperCase()
.value();
使用链接时有以下三种方法:
有条件地附加到链
您必须保存部分链,然后有条件地对其调用所需的方法并保存输出。然后你可以继续正常处理:
const isUpperCase = true;
const users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
let chain = _.chain(users)
.sortBy('age')
.map(function(o) {
return o.user + ' is ' + o.age;
})
.head();
// Only add this method if isUpperCase above is set to true
if (isUpperCase) {
chain = chain.upperCase();
}
const youngest = chain.value();
console.log(youngest);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
使用 .thru()
添加手动步骤
.thru()
方法将通过自定义函数传递链的值和 return 结果。这允许您在自定义函数内应用条件处理。
const isUpperCase = true;
const users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
const youngest = _.chain(users)
.sortBy('age')
.map(function(o) {
return o.user + ' is ' + o.age;
})
.head()
.thru(function(o) {
if (isUpperCase)
return _.upperCase(o);
return o;
})
.value();
console.log(youngest);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
有条件地处理链的结果
这可能更容易,因为您不需要篡改链本身。根据条件,有值后可以申请upperCase
const isUpperCase = true;
const users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
const result = _.chain(users)
.sortBy('age')
.map(function(o) {
return o.user + ' is ' + o.age;
})
.head()
.value();
let youngest = result;
// Only add this method if isUpperCase above is set to true
if (isUpperCase){
youngest = _.upperCase(result);
}
console.log(youngest);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
使用 flow
的功能组合
还有一种替代方法,即使用 functional composition to set up the processing chain. Then you can add or alter parts of this easily and then finally execute it against any dataset. The Lodash FP 分布提供非常适合此任务的现成函数。所有函数都被柯里化,数据参数移到最后。
const { flow, sortBy, map, head, upperCase } = _;
const isUpperCase = true;
const users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
const chain = [
sortBy('age'),
map(function(o) {
return o.user + ' is ' + o.age;
}),
head
];
// Only add this method if isUpperCase above is set to true
if (isUpperCase) {
chain.push(upperCase);
}
const youngest = flow(...chain)(users)
console.log(youngest);
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>