本文介绍了数组上的 ramda 函数 javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const censusMembers = Object.freeze([{编号:1,名称:'鲍勃'}, {编号:2,姓名:'苏'}, {编号:3,name: '玛丽',家庭 ID:2}, {编号:4,name: '伊丽莎白',家庭 ID:6}, {编号:5,姓名:'汤姆'}, {编号:6,名称:'吉尔'}, {编号:7,name: '约翰',家庭 ID:6}]);

在这个数组中,Adependent 可以通过family_id 的存在来确定.family_id 是对该成员所依赖的员工 ID 的引用(例如在人口普查成员列表中玛丽"是苏"的依赖者)

如何构建一个函数,该函数接受一个 id 和成员数组(人口普查成员)并返回属于具有该 id 的用户的所有家属.

如果 id 是依赖的,或者不在 censusMember 数组中,那么函数应该返回 null.

如果没有依赖项,则该函数应返回一个空数组.

例如:

如果我输入 id 6然后输出应该是

[{"id":4,"name":"Elizabeth","household_id":6},{"id":7,"name":"John","household_id":6}]
解决方案

以下代码似乎可以满足您的需求:

const {curry, find, propEq, has, filter} = Rconst homeMembers = curry((人口普查,id) => {const person = find(propEq('id', id), 人口普查);归来者?has('household_id', 人)?空值:过滤器(propEq('household_id',id),人口普查): 空值})var censusMembers = Object.freeze([{id: 1, name: 'Bob'},{id: 2, name: 'Sue' },{id: 3, name: 'Mary', family_id: 2 },{id: 4, name: 'Elizabeth', family_id: 6},{id:5,姓名:'汤姆'},{id: 6, name: 'Jill'},{id: 7, name: 'John', family_id: 6}])const 家庭成员 = 家庭成员(人口普查成员)控制台日志(家庭户(6))//=>[//{id: 4, name: 'Elizabeth','household_id': 6},//{id: 7, name: 'John', 'household_id': 6}//]console.log(householders(7))//=>空值console.log(householders(8))//=>空值console.log(householders(5))//=>[]
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>

但我建议您可能需要重新考虑此 API.当什么都没有找到时,空数组是一个完全合理的结果.让它在某些情况下返回 null 会使输出更难使用.例如,如果您想检索家庭成员的姓名列表,您可以简单地编写 constresidenterNames = pipe(householders, prop('name')).或者,如果您的函数总是返回一个列表,您可以这样做.

像这样让一个函数返回多个类型更难理解,也更难维护.请注意以下版本要简单得多,它总是返回一个(可能是空的)列表:

const members = curry((census, id) => filter(propEq('household_id', id), census))
const censusMembers = Object.freeze([
    {
        id: 1,
        name: 'Bob'
    }, {
        id: 2,
        name: 'Sue'
    }, {
        id: 3,
        name: 'Mary',
        household_id: 2
    }, {
        id: 4,
        name: 'Elizabeth',
        household_id: 6
    }, {
        id: 5,
        name: 'Tom'
    }, {
        id: 6,
        name: 'Jill'
    }, {
        id: 7,
        name: 'John',
        household_id: 6
    }
]);

In this array, A dependent can be determined by the presence of a household_id. The household_id is a reference to the ID of the employee that that member is a depended of (ex in the censusMembers list 'Mary' is a dependent of 'Sue')

How to build a function that takes in an id and the array of members(census members) and returns all dependents that belong to the user that has that id.

If the id is of a dependent, or isn't in the censusMember array then the function should return null.

If there are no dependents then the function should return an empty arrray.

for example:

if I give input as id 6then output shoul be

[
{"id":4,"name":"Elizabeth","household_id":6},
{"id":7,"name":"John","household_id":6}
]
解决方案

Here is code that seems to do what you would like:

const {curry, find, propEq, has, filter} = R

const householdMembers = curry((census, id) => {
  const person = find(propEq('id', id), census);
  return person
    ? has('household_id', person)
      ? null
      : filter(propEq('household_id', id), census)
    : null
})


var censusMembers = Object.freeze([
  {id: 1, name: 'Bob'},
  {id: 2, name: 'Sue' },
  {id: 3, name: 'Mary', household_id: 2 },
  {id: 4, name: 'Elizabeth', household_id: 6},
  {id: 5, name: 'Tom'},
  {id: 6, name: 'Jill'},
  {id: 7, name: 'John', household_id: 6}
])

const householders = householdMembers(censusMembers)

console.log(householders(6))
//=> [
//     {id: 4, name: 'Elizabeth','household_id': 6},
//     {id: 7, name: 'John', 'household_id': 6}
//   ]
console.log(householders(7))  //=> null
console.log(householders(8))  //=> null
console.log(householders(5))  //=> []
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>

But I would suggest that you might want to rethink this API. The empty array is a perfectly reasonable result when nothing is found. Making it return null for some of these cases makes the output much harder to use. For instance, if you wanted to retrieve the list of names of household members, you could simply write const householderNames = pipe(householders, prop('name')). Or you could do this if your function always returned a list.

Having a single function return multiple types like this is much harder to understand, and much, much harder to maintain. Note how much simpler the following version is, one that always returns a (possibly empty) list:

const members = curry((census, id) => filter(propEq('household_id', id), census))

这篇关于数组上的 ramda 函数 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 14:01
查看更多