Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
How can I access a specific attribute of an object that is within an array of objects?

I have a function called "howmanyOnline" that receives an object users. Each user has a property online which is a boolean. It should return the number of users with the property online equal to true.

For example How many Online (users) returns 2 this the object of object:

    let users = {
      Victor: {
              age: 33,
             online: true
     },
      joan: {
         age: 25,
         online: true
      },
     frank: {
         age: 25,
         online: false
     },
     Emy: {
          age: 24,
          online: false
       }
  }; 

   

    function howmanyOnline(users) {

  }

How can I do this?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

There are lots of ways you can do this, but the most common are one of either:

Use array.filter after grabbing the values from the object to filter down to only the users who are online, and then take the count of that array. This method takes a callback function, and is the more 'javascripty' way of doing this.

const values = {one: {a: 1}, two: {a: 2}, three: {a: 3}};
const arr = Object.values(values);
// filter down to only the elements which a value >= 2 under key 'a'
const filtered = arr.filter(obj => obj.a >= 2);
filtered.length; // 2

Start with a variable initialized to 0, then map or otherwise iterate across the object and increment the variable by 1 for every user who is online.

let count = 0;
const values = {one: {a: 1}, two: {a: 2}, three: {a: 3}};
// check if the value under key 'a' is >= 2, if so add 1 to count,
// otherwise add 0
for(value in values){count += values[value]['a'] >= 2 ? 1 : 0};
count; // 2

about 4 years ago · Juan Pablo Isaza Relatório

0

let users = {
  Victor: {
    age: 33,
    online: true
  },
  joan: {
    age: 25,
    online: true
  },
  frank: {
    age: 25,
    online: false
  },
  Emy: {
    age: 24,
    online: false
  }
};

function howmanyOnline(users) {
  let count = 0;
  for (const { online } of Object.values(users)) {
    if (online) count += 1;
  }
  return count;
}

console.log(howmanyOnline(users));

about 4 years ago · Juan Pablo Isaza Relatório

0

This should work by using a for loop to get the online value and a if statement to check if it is true.

let users = {
              Victor: {
                      age: 33,
                     online: true
             },
              joan: {
                 age: 25,
                 online: true
              },
             frank: {
                 age: 25,
                 online: false
             },
             Emy: {
                  age: 24,
                  online: false
               }
          }; 
    let number =0
           

            function howmanyOnline(users) {
        for(let value of Object.values(users)){
         if(value.online)
         number++
        }
        return number
          }
          console.log(howmanyOnline(users))

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda