[英]Filter & map an array with Ramda
我正在使用 Ramda 在他们的类别数组中获得'Prem League'
的球队。 我的代码如下所示,并且可以正常工作。
import { pipe, map, filter } from 'ramda'
const teams = [
{name: 'Liverpool', id: '1', categories: ['Prem League']},
{name: 'Man Utd', id: '2', categories: ['Blue Square']},
{name: 'Sheff Utd', id: '2', categories: ['Prem League']},
]
const getTeamOptions = pipe(
filter((team) => team.categories.includes('Prem League')),
map((team) => ({ label: team.name, value: team.id }))
);
getTeamOptions(teams)
但是我想删除team
作为 filter 和map
的参数。
我尝试了以下但得到prop(...).includes is not a function
filter(prop('categories').includes('Prem League')),
理想情况下,我也会尝试从map
中删除team
,但也许这不是必需的。
这些变化的原因是我一直在学习这门课程,它建议prop
等作为最佳实践。
您可以使用 R.includes 来检查该值是否存在。 您可以使用 R.applySpec 生成新的 object:
const { pipe, filter, prop, includes, map, applySpec } = R; const getTeamOptions = val => pipe( filter(pipe(prop('categories'), includes(val))), map(applySpec({ label: prop('name'), value: prop('id') })) ); const teams = [{"name":"Liverpool","id":"1","categories":["Prem League"]},{"name":"Man Utd","id":"2","categories":["Blue Square"]},{"name":"Sheff Utd","id":"2","categories":["Prem League"]}]; const result = getTeamOptions('Prem League')(teams); console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
这也将帮助您以无点方式使用 ramda 解决它...
const hasPremLeague = R.where({ categories: R.includes('Prem League') }); const toOption = R.applySpec({ label: R.prop('name'), value: R.prop('id') }); const getTeamOptions = R.into([], R.compose( R.filter(hasPremLeague), R.map(toOption), )); // --- const teams = [ {name: 'Liverpool', id: '1', categories: ['Prem League']}, {name: 'Man Utd', id: '2', categories: ['Blue Square']}, {name: 'Sheff Utd', id: '2', categories: ['Prem League']}, ]; console.log( getTeamOptions(teams), );
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
const premLeague = R.equals('Prem League');
const premLeagueInArray = R.any(premLeague);
const categories = R.path(['categories']);
const isPremLeagueInArray = R.pipe(
categories,
premLeagueInArray,
);
const teams = [
{ name: "Liverpool", id: "1", categories: ["Prem League"] },
{ name: "Man Utd", id: "2", categories: ["Blue Square"] },
{ name: "Sheff Utd", id: "2", categories: ["Prem League"] },
];
const premLeagueTeam = [
{ name: "Liverpool", id: "1", categories: ["Prem League"] },
{ name: "Sheff Utd", id: "2", categories: ["Prem League"] },
];
const transducer = R.compose(R.filter(isPremLeagueInArray));
const getPremLeagueTeam = R.transduce(transducer, R.flip(R.append), []);
R.equals(getPremLeagueTeam(teams), premLeagueTeam);
您也应该考虑使用非 Ramda 选项。
这可能是也可能不是Array#flatMap
的滥用,但我认为这是可以接受的:filter + map = flatMap
假设您想将 10 添加到偶数并排除奇数:
[1, 2, 3, 4].flatMap(n => n % 2 === 0 ? n + 10 : []);
//=> [12, 14]
关于无点风格,还有一点需要说明。 这很好,但有时它会妨碍您。 例如,它不允许您利用一些不错的 ES6 结构。 例如解构:
const getTeamOptions =
teams =>
teams.flatMap
( ({name: label, id: value, categories}) =>
categories.includes('Prem League')
? { label, value }
: []
);
getTeamOptions
( [ {name: 'Liverpool', id: '1', categories: ['Prem League']}
, {name: 'Man Utd', id: '2', categories: ['Blue Square']}
, {name: 'Sheff Utd', id: '2', categories: ['Prem League']}
]
);
//=> [ {label: "Liverpool", value: "1"}
//=> , {label: "Sheff Utd", value: "2"} ]
为了完整起见,这里有一个使用Array#reduce
的变体:
const getTeamOptions =
teams =>
teams.reduce
( (acc, {name: label, id: value, categories}) =>
categories.includes('Prem League')
? (acc.push({ label, value }), acc)
: acc
, []
);
不要误会我的意思! 拉姆达绝对是惊人的。 当我第一次遇到这个库时,我想用它重写我的所有代码,然后我发现了无点风格并再次重写了所有代码。 最后,我完全失去了对代码的精神控制,这是一个问题。 你应该只在 Ramda 为你服务的时候才使用它。 在这种情况下,你可以不诚实。
我喜欢以可组合的方式执行此操作,但目前您使用的是pipe
,所以我使用pipe
执行此操作以保持一致性。 下面是一个有效的解决方案
const teams = [ { name: 'Liverpool', id: '1', categories: ['Prem League'] }, { name: 'Man Utd', id: '2', categories: ['Blue Square'] }, { name: 'Sheff Utd', id: '2', categories: ['Prem League'] } ] const getTeamOptions = pipe( filter( pipe( prop('categories'), includes('Prem League') ) ), map( pipe( props(['name', 'id']), zipObj(['label', 'value']) ) ) ) console.log(getTeamOptions(teams))
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script> <script>const { pipe, map, filter, prop, includes, zipObj, props } = R</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.