const articles = [
{title: 'title 1', published: false, pages: 30, tags: {name: 'work', id: 1, visibility: "everyone"}},
{title: 'title 2', published: false, pages: 25, tags: {name: 'home', id: 3, visibility: "myself"}},
{title: 'title 3', published: true, pages: 30, tags: {name: 'vacation', id: 5, visibility: "myself"}}
];
我的JSON数据看起来像这样,我需要根据published
为true和visibility
为我的值进行过滤。如何有效地做到这一点?希望对此有所帮助。
到目前为止,我只能使用单个属性进行过滤
R.filter(R.propEq("published", false))(articles);
答案 0 :(得分:1)
类似这样的东西:
R.filter(
R.allPass([
R.propEq("published", false),
R.pathEq(["tags", "visibility"], "myself")
])
)(articles);
答案 1 :(得分:1)
您可以使用一个spec对象:
const predicate = R.where({
published: R.equals(true),
tags: R.where({
visibility: R.equals('myself'),
}),
});
// ==
const data = [
{title: 'title 1', published: false, pages: 30, tags: {name: 'work', id: 1, visibility: "everyone"}},
{title: 'title 2', published: false, pages: 25, tags: {name: 'home', id: 3, visibility: "myself"}},
{title: 'title 3', published: true, pages: 30, tags: {name: 'vacation', id: 5, visibility: "myself"}}
];
console.log(
R.filter(predicate, data),
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js" integrity="sha256-buL0byPvI/XRDFscnSc/e0q+sLA65O9y+rbF+0O/4FE=" crossorigin="anonymous"></script>