[Ramda] Filter, Reject and Partition

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Ramda] Filter, Reject and Partition相关的知识,希望对你有一定的参考价值。

We‘ll learn how to get a subset of an array by specifying items to include with filter, or items to exclude using reject. We‘ll also look at how to get the results from both filter and reject, neatly separated with partition.

 

// we don‘t need to require in Plunker!
//const R = require(‘ramda‘)

const pets = [
  {name: ‘Spike‘, type: ‘dog‘},
  {name: ‘Mittens‘, type: ‘cat‘},
  {name: ‘Rover‘, type: ‘dog‘},
  {name: ‘Fluffy‘, type: ‘cat‘},
  {name: ‘Fido‘, type: ‘dog‘}
]

const dogCheck = pet => pet.type == ‘dog‘

// const result = R.filter(dogCheck, pets)
// const result = R.reject(dogCheck, pets)

const result = R.partition(dogCheck, pets)

console.log(result)
document.getElementById(‘output‘).innerhtml = `${JSON.stringify(result)}`

 

/*
[
[{"name":"Spike","type":"dog"},{"name":"Rover","type":"dog"},
{"name":"Fido","type":"dog"}],

[{"name":"Mittens","type":"cat"},{"name":"Fluffy","type":"cat"}]
]
*/

 

以上是关于[Ramda] Filter, Reject and Partition的主要内容,如果未能解决你的问题,请参考以下文章

Vue : Expected the Promise rejection reason to be an Error

[Ramda] Basic Curry with Ramda

[Ramda] Sort, SortBy, SortWith in Ramda

[Ramda] Change Object Properties with Ramda Lenses

[Ramda] Declaratively Map Predicates to Object Properties Using Ramda where

[Ramda] Rewrite if..else with Ramda ifElse