如何在这种情况下撰写

问题描述 投票:1回答:1
var articles = [
  {
    title: 'Everything Sucks',
    author: { name: 'Debbie Downer' }
  },
  {
    title: 'If You Please',
    author: { name: 'Caspar Milquetoast' }
  }
];

var names = _.map(_.compose(_.get('name'), _.get('author'))) 
// returning ['Debbie Downer', 'Caspar Milquetoast']

现在基于上面给出的articles和函数names,创建一个布尔函数,说明给定的人是否写了任何文章。

isAuthor('New Guy', articles) //false
isAuthor('Debbie Downer', articles)//true

我的尝试在下面

var isAuthor = (name, articles) => {
    return _.compose(_.contains(name), names(articles))
};

然而,它在jsbin失败,错误如下。也许有人可以尝试解释我的尝试出了什么问题,这样我就可以从错误中吸取教训

未捕获预期的错误等于函数(n,t){return r.apply(this,arguments)}

javascript functional-programming ramda.js
1个回答
1
投票

Compose返回一个函数,因此您需要将articles传递给该函数。 Compose会将articles传递给getNames,并将getNames的结果传递给contains(name)(也返回一个函数),它将处理作者名称,并返回布尔值result

const { map, path, compose, contains } = R

const getNames = map(path(['author', 'name']))

const isAuthor = (name) => compose(
  contains(name),
  getNames
)

const articles = [{"title":"Everything Sucks","author":{"name":"Debbie Downer"}},{"title":"If You Please","author":{"name":"Caspar Milquetoast"}}]

console.log(isAuthor('New Guy')(articles)) //false
console.log(isAuthor('Debbie Downer')(articles)) //true
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.