假设我有一个这样的对象数组:
[{
away: "Seattle Seahawks",
home: "Kansas City Chiefs",
},
{
away: "Houston Texans",
home: "San Francisco 49ers",
},
{
away: "Dallas Cowboys",
home: "Los Angeles Rams",
}]
Run Code Online (Sandbox Code Playgroud)
要求:
搜索每个对象和关键字49er并让它返回对象 #2。
在每个对象中搜索关键字cow并让它返回对象 #3。
在每个对象中搜索关键字an并让它返回所有三个对象。
在 lodash 中实现这一目标的最佳方法是什么?谢谢!
我的解决方案_.flow:
const my_arr = [{
away: "Seattle Seahawks",
home: "Kansas City Chiefs",
},
{
away: "Houston Texans",
home: "San Francisco 49ers",
},
{
away: "Dallas Cowboys",
home: "Los Angeles Rams",
}]
function flowFilter(array, substr) {
return _.filter(array, _.flow(
_.identity,
_.values,
_.join,
_.toLower,
_.partialRight(_.includes, substr)
));
}
const one = flowFilter(my_arr, '49er');
const two = flowFilter(my_arr, 'cow');
const three = flowFilter(my_arr, 'an');
console.log('one', one);
console.log('two', two);
console.log('three', three);
Run Code Online (Sandbox Code Playgroud)
https://jsfiddle.net/1321mzjw/
| 归档时间: |
|
| 查看次数: |
2428 次 |
| 最近记录: |