JavaScript Map-Reduce : How to count in a collection

Important edit : I can't use filter - the purpose is pedagogic.

I have an array in which I would want to count the number of its elements that verify a boolean, using only map and reduce.

Count of the array's size

I already wrote something that counts the array's size (ie. : the number of all of its elements), using reduce :

const array_numbers = [12, 15, 1, 1]; // Size : 4
console.log(
              array_numbers.reduce((acc) => {
                return acc + 1;
              }, 0)
);

Count of the array's elements checking a boolean condition

Now I would want to count only the elements that verify a boolean condition. Thus, I must use map before reduce and the elements of the map's returned array will be only the good elements.

So I wrote this code but it doesn't work... Indeed, I put null when I encounter a not-good element (and null is counted as en element unfortunately).

NB : here, the boolean condition is "is the element even ? (%2 == 0)".

const array_numbers = [12, 15, 1, 1]; // Size : 4
console.log(
              array_numbers.map((current_value) => {
                if(current_value % 2 == 0) {
                  return current_value;
                }
                return null;

              }).reduce((acc) => {
                return acc + 1;

              }, 0)
);
Answer:1

Array#filter the array and check the length:

const array_numbers = [12, 15, 1, 1];
const result = array_numbers.filter((n) => n % 2 === 0).length;

console.log(result);
Answer:2

You can use Array.prototype.filter to filter the even numbers - and you don't need the reduce() function - you can use length of the array returned by the filter() function.

Or you can use reduce() method alone like below:

See demos below:

const array_numbers = [12, 15, 1, 1]; // Size : 4

// using filter
console.log(
  array_numbers.filter((current_value) => {
    return current_value % 2 == 0;
  }).length
);

// using reduce
console.log(
  array_numbers.reduce((prev, curr) => {
    return curr % 2 == 0 ? prev + 1 : prev;
  }, 0)
);
Answer:3

Clicking #input multiple times will upload a file each time. Clicking #button which in turn triggers #input will only upload a file the first time (and not at all if #input was first clicked). I ...

Clicking #input multiple times will upload a file each time. Clicking #button which in turn triggers #input will only upload a file the first time (and not at all if #input was first clicked). I ...

  1. jquery trigger only once
  2. jquery trigger only one time
  3. jquery trigger only works once
  4. jquery trigger only one event
  5. jquery fire only once
  6. jquery trigger click only once
  7. jquery trigger event only once
  8. jquery trigger function only once
  9. jquery trigger change only once
  10. jquery trigger click only works once
  11. jquery only fire event once
  12. jquery trigger change event only once

I'm using the request library in node.js, which downloads an HTML body response, but does so in chunks. Currently I'm just concatenating them into a dataChunks variable and using a hacked-together ...

I'm using the request library in node.js, which downloads an HTML body response, but does so in chunks. Currently I'm just concatenating them into a dataChunks variable and using a hacked-together ...

  1. nodejs convert http to https

i have form that's announcing user for error, warning or new content on my site. I have three buttons on class ann-right and each button have its color. When i click the button, the button input on ...

i have form that's announcing user for error, warning or new content on my site. I have three buttons on class ann-right and each button have its color. When i click the button, the button input on ...

I have a input box. I enter the search term and the values get returned. What I am trying to do is click the dynamic button with the value attached to it. However when I click on the button it reloads ...

I have a input box. I enter the search term and the values get returned. What I am trying to do is click the dynamic button with the value attached to it. However when I click on the button it reloads ...

  1. jquery dynamic button click
  2. jquery dynamic button click handler
  3. jquery dynamic button event
  4. jquery dynamic button not working
  5. jquery dynamic button click function
  6. jquery dynamic button create
  7. jquery dynamic button bind
  8. jquery button dynamic id
  9. jquery dynamic radio button click event
  10. jquery dialog dynamic buttons
  11. jquery dynamic radio button
  12. jquery get dynamic button id
  13. jquery dialog dynamic button text
  14. jquery dynamic add button
  15. jquery dynamic change button text
  16. jquery dynamic radio button change
  17. jquery dynamic radio button checked
  18. jquery datatable dynamic button
  19. jquery disable dynamic button
  20. jquery ui dialog dynamic buttons