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

103
Visualizações
Implement condition in for..in loop

I am looking at for..in tutorial from MDN documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

The example iterates over the object's properties and prints all. I am trying to print only if value of object's property is 3. Can someone please help me understand where I am going wrong. And also if possible explain.

const object = { a: 1, b: 2, c: 3 };

for (const property in object.c == 3) {
  console.log(`${property}: ${object[property]}`);
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

for...in loops can't have conditions.¹ You have to implement the condition as an if within the loop:

for (const property in object) {
    if (object[property] === 3) {
        console.log(`${property}: ${object[property]}`);
    }
}

I've assumed you want any property with the value 3, not just c (since there'd be no need for a loop).


¹ "for...in loops can't have conditions" So why didn't you get an error? Because your code was syntactically correct, it just didn't do what you expected. 🙂

The for (const property in object.c == 3) is evaluated like this:

  1. Calculate the result of object.c == 3. Now we have:

    for (const property in true) // or `in false`
    
  2. Since for...in only works on objects, convert the boolean primitive to a Boolean object. Now we have:

    for (const property in new Boolean(true)) // or `in new Boolean(false)`
    
  3. Since Boolean objects have no enumerable properties, the loop never does anything.

about 4 years ago · Juan Pablo Isaza Relatório

0

You have to do every task step by step. The for in loop is for iteration. After that, you use an if conditional to check if the property is equals to 3. Check the code below:

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
    if(object[property]===3)
        console.log(`${property}: ${object[property]}`);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

looks like you want to use for and if at same time.

You should try this:

const object = { a: 1, b: 2, c: 3 };

for (const property in object) {
    if(object[property]==3){
        console.log(`${property}: ${object[property]}`);
    }
}
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