0

I have a object that looks like this:

const people = {
  men: 4,
  women: 2,
  total: 6,
  participants: {
    1: 4,
    2: 1,
    3: 0
  }
};

I am trying to achieve such result:

1 participants count 4
2 participants count 1
3 participants count 0 <- Hide me if count === 0

What is the best way to achieve this? using map or forEach or for loop? Any help is appreciated, I have tried:

for (let key in people.participants) {
 console.log("count",people.participants[key],"key",key;
}

which seems to be working except I cannot use it in React component like i would with map ...

Ilanus
  • 6,690
  • 5
  • 13
  • 37

1 Answers1

-1

My favourite way :)

people.participants.forEach(function(elem, index, array) {
    if(elem === 0) {
        hideElem(elem);
    }
}, this);

function hideElem(elem) {
    //do the stuff here
}
Nikita
  • 1,019
  • 2
  • 15
  • 39