Redeem values from an array when the value is greater than x

Asked

Viewed 50 times

0

I have an array that says, which seller and how many sales he made:

array{
'a'=> 1
'b'=> 2
'c'=> 16
'd'=> 4
..
}

I need to know how many sellers have sold more than 4 products.

2 answers

2

You can use the function map traversing the array and checking whether the amount is greater than or equal to 4:

You can use it with an object array, or even an array only of numbers by checking which numbers are greater than or equal to 4

var array = [
{vendedor: 'Joao', quantidade: 1},
{vendedor: 'maria', quantidade: 5},
{vendedor: 'jose', quantidade: 3},
{vendedor: 'claudio', quantidade: 7},
{vendedor: 'Joana', quantidade: 2}
]


array.map(item => {
  if(item.quantidade >= 4) 
    console.log(item)
})

var array2 = [1, 4, 5 , 10, 21, 3, 0, 3, 2];

array2.map(item => {
  if(item >= 4) 
    console.log(item)
})

2


You can go through the array by checking occurrences of sales number greater than 4. As in the example below:

<?php

$vendedores = array(
    'a'=> 1,
    'b'=> 2,
    'c'=> 16,
    'd'=> 4);

$count = 0;

foreach($vendedores as $v){
    if($v > 4) $count++;
}

echo $count;

?>
  • That’s right, Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.