[英]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.