码迷,mamicode.com
首页 > 其他好文 > 详细

[Ramda] Filter, Reject and Partition

时间:2016-09-24 07:00:25      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5902304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!