使用Ramda过滤和映射数组

时间:2020-06-19 09:35:06

标签: javascript ecmascript-6 ramda.js

我正在使用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')),

理想情况下,我也会尝试从team中删除map,但这也许不是必需的。

做出这些更改的原因是,我一直遵循this course,并建议prop等作为最佳实践。

5 个答案:

答案 0 :(得分:1)

您可以使用R.includes检查该值是否存在。您可以使用R.applySpec生成新对象:

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>

答案 1 :(得分:1)

这也将帮助您以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>

答案 2 :(得分:0)

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);

答案 3 :(得分:0)

您也应该考虑使用非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绝对令人惊奇。当我第一次遇到这个库时,我想用它重写我的所有代码,然后我发现了无点样式并再次重写了所有内容。最后,我完全失去了对代码的精神控制,这是一个问题。只有在可以满足您的要求时,才应使用Ramda。在这种情况下,您可以不用诚实。

答案 4 :(得分:0)

我喜欢以可组合的方式执行此操作,但是目前您正在使用x,因此我会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))