1

  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
    }
    ]);

This is my array I want to count the number of elements which has household id using ramda function ? how can i do that ?

5
  • Why Ramda? this is possible using vanilla JS. Commented Oct 25, 2018 at 16:53
  • I am exploring ramda, can you please help using ramda ? Commented Oct 25, 2018 at 16:57
  • What Ramda techniques have you tried so far, then? Commented Oct 25, 2018 at 16:57
  • R.pipe(R.filter(R.propSatisfies(Boolean, ‘household_id’), R.length) Commented Oct 25, 2018 at 16:58
  • This should work fine. I think you're simply missing a second parentheses before , R.length. But see my answer for a cleaner solution. Commented Oct 25, 2018 at 17:08

3 Answers 3

3

You can also use R.countBy to count all items that have/don't have household_id using R.has(), and than get the count for true using R.prop():

const { pipe, countBy, has, prop } = R;

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}]);

const countHouseholders = pipe(
  countBy(has('household_id')),
  prop('true'),
);

const result = countHouseholders(censusMembers);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

0

has looks to be what you're missing:

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 countHouseholders = R.pipe(R.filter(R.has('household_id')), R.length)

console.log(countHouseholders(censusMembers))
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>

8 Comments

How to find number of elements which does not have household_id ?
You can replace filter by reject in the above, or wrap has('household_id') in complement. Either should work.
I am struggling on this how to Builb function that takes in an id and censusMembers 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. example: if I give id as 6 then I should get this object [{"id":4,"name":"Elizabeth","household_id":6},{"id":7,"name":"John","household_id":6}]
Please ask another question for that. Comments are meant for clarifications, etc. Another question would be fine.
It says I have to wait 90 minutes so can you please reply here ?
|
0

Don't use the function filter to count stuff because you're creating an array only to get the length.

You can use the function R.reduce and within the handler, check for the key household_id

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}]);

// With household_id
//                                      (true is coerced to 1) = 1
console.log("With:", R.reduce((a, c) => ('household_id' in c) + a, 0, censusMembers));

// Without household_id
//                                         !(false is coerced to 0) = 1
console.log("Without:", R.reduce((a, c) => !('household_id' in c) + a, 0, censusMembers));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>

6 Comments

There is little reason to introduce Ramda to a solution that works fine without it. (censusMembers.reduce(fn, 0) versus R.reduce(fn, 0, censusMembers).) Ramda is all about building and composing functions. I know that this is a learning exercise for the OP, but Ramda is not meant to be a Underscore/lodash replacement; it's meant to make it easier to write FP in JS.
@ScottSauyet for some reason that function is available in Ramda.
I am struggling on this how to Builb function that takes in an id and censusMembers 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. example: if I give id as 6 then I should get this object [{"id":4,"name":"Elizabeth","household_id":6},{"id":7,"name":"John","household_id":6}]
@Ele: The function is there so that you could write, for instance, const sum = reduce(add, 0), creating a new function from an old one. It adds little of use to say const myTotal = reduce(add, 0, myNumbers). There's nothing wrong with it, precisely. I would write it that way if I was already using Ramda. But it's not really what Ramda is designed for.
@RishabhSrivastava: Again, don't ask that in comments. Please ask a new question if you are looking for something so different from the original question.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.