Count Within an Array

2

I want to tell only one type of product, which is the best way to do it, Example I want you to print the number of "ORANGE" = 2 and not the whole array:

 var fruits = ["Banana", "Orange", "Apple", "Mango","Orange"];
fruits.length;

console.log(fruits.length);
    
asked by Rafael Pereira 30.07.2018 в 20:15
source

4 answers

3

Using filter :

var fruits = ["Banana", "Orange", "Apple", "Mango","Orange"];
var naranjas = fruits.filter(function(value, index) {return value == "Orange";})
console.log(naranjas.length);

First you filter all the results that are "Orange" and then print the length. Basically a sub-array of the original array is being created.

Or with the new notation:

var fruits = ["Banana", "Orange", "Apple", "Mango","Orange"];
var naranjas = fruits.filter(fruit => fruit == "Orange")
console.log(naranjas.length);

Update:

It occurs to me that to count fruits the simplest thing is to create an object that stores the quantities:

var fruits = ["Banana", "Orange", "Apple", "Mango","Orange"];
var fruits_counter = {};

fruits.forEach(function(fruit) {
  if (!fruits_counter.hasOwnProperty(fruit)) {
    fruits_counter[fruit] = 0
  }
  fruits_counter[fruit] += 1;
});

console.log('Mangos: ' + fruits_counter.Mango);
console.log('Naranjas: ' + fruits_counter.Orange);
console.log('Manzanas: ' + fruits_counter.Apple);
console.log('Melocotones: ' + (fruits_counter.Peach || 0));

In each iteration pass, the amount in 0 is initialized if that property does not exist in the object (using hasOwnProperty ).

    
answered by 30.07.2018 / 20:20
source
1

You have to use a loop and a counter like this:

var fruits = ["Banana", "Orange", "Apple", "Mango","Orange"];
var oranges = 0;

fruits.forEach(function(fruit) {
  if (fruit == "Orange") {
    oranges++;
  }
});

console.log(oranges);
    
answered by 30.07.2018 в 20:17
1

@alfredo's response totally destroys the structure of functional programming using avoid effects by mutating values outside the function, the response of @ César is very good is the most common, you can also use an accumulator that has reduced.

const fruits = ["Banana", "Orange", "Apple", "Mango","Orange"];

const count = fruits.reduce((acumulator, current) => {
  return current === 'Orange' ? acumulator + 1 : acumulator;
}, 0);
console.log(count);

Functional programming does not iterate, it uses recursion here, an example of how the map works inside

const map = ([head, ...tail], fn) => (
  typeof head === 'undefined' ? [] : [fn(head), ...map(tail, fn)]
);

const result = map([1, 2, 3], (x) => x * 2)

console.log(result);

As you can see, an Array returns as does filter, that is why using .length with filter returns the count of the searched value

    
answered by 30.07.2018 в 21:37
0

I leave this example with the map method

 let fruits = ["Banana", "Orange", "Apple", "Mango","Orange"];

let counter = 0
fruits.map(el => {
  if(el === 'Orange'){
    counter = counter + 1
  }
})

console.log('There are ${counter} oranges')
  

At the end with template strings command syntax print the value of   the variable in a text string; I also declare the variable counter > out of path of map() since otherwise for each iteration > the variable would be redeclared which would preclude the result > what do you want to get

    
answered by 30.07.2018 в 20:22