Ramda:如何使用过滤功能保持原始结构

时间:2017-03-02 11:13:34

标签: javascript ramda.js

我想做一个过滤功能,如下所示:

配置:

const arr = {
  id: 888,
  name: 'Library',
  bookLists: [{
      "name": "Book 1",
      "booked": "Y"
    }, {
      "name": "Book 2",
      "booked": "N"
    }, {
      "name": "Book 3",
      "booked": "Y"
    }]
  };

然后我尝试使用标记'预订=== Y'

过滤图书清单
R.compose(R.filter(x => x.booked === 'Y'), R.prop('bookLists'))(arr)

我得到了结果,这是正确的

[{"booked": "Y", "name": "Book 1"}, {"booked": "Y", "name": "Book 3"}]

然而,这是我想要的一部分。我怎样才能达到这样的结果?

{
  id: 888,
  name: 'Library',
  bookLists: [{
      "name": "Book 1",
      "booked": "Y"
    }, {
      "name": "Book 3",
      "booked": "Y"
    }]
  }

1 个答案:

答案 0 :(得分:2)

您可以使用R.evolve

R.evolve({bookLists: R.filter(x => x.booked === 'Y')})(arr)

结果将是:

{
  id: 888,
  name: 'Library',
  bookLists: [{
      "name": "Book 1",
      "booked": "Y"
    }, {
      "name": "Book 3",
      "booked": "Y"
    }]
  }

参见相关的DOCS here