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

151
Visualizações
Compare objects within a array

I have an array

[{"name": "pay1","isEnabled": true},{"name": "pay2","isEnabled": false}]

I need to console the value where isEnabled is true.

I have tried with forEach method but is not working if both value are true.

Because forEach iterates over each object so it will get one value at a time.

Below is my code

const arr = [{"name": "pay1","isEnabled": true},{"name": "pay2","isEnabled": true}]

arr.forEach(item => {

              if ((item.name === 'pay1' && item.isEnabled) && (item.name === 'pay2' && item.isEnabled)) {
                console.log('both enabled')
              } else if (item.name === 'pay1' && item.isEnabled) {
                console.log('pay1 enabled')
              } else if(item.name === 'pay2' && item.isEnabled){
             console.log('pay2 enabled')
              }
            })

How can i resolve this.

Thanks in advance........

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

one way is to filter all enabled country and display them

const arr = [{"name": "pay1","isEnabled": true},{"name": "pay2","isEnabled": true},,{"name": "pay2","isEnabled": false}]


const arrFilt = arr.filter(oneCountry => oneCountry.isEnabled);

arrFilt.forEach(item => {
   console.log(`${item.name} enabled`)
})

about 4 years ago · Santiago Trujillo Relatório

0

There is no need to run a loop.

Just find the elememnt with name having pay1 and name having pay1. Check the isEnabled of each node. If its true for both name, both are enabled or only one enabled.

const arr = [{ "name": "pay1", "isEnabled": true }, { "name": "pay2", "isEnabled": true }];
const item1 = arr.find(node => node.name === "pay1");
const item2 = arr.find(node => node.name === "pay2");
if (item1.isEnabled && item2.isEnabled) {
  console.log('both enabled')
} else if (item1.isEnabled) {
  console.log('pay1 enabled')
} else if (item2.isEnabled) {
  console.log('pay2 enabled')
} else {
  console.log('none enabled')
}

about 4 years ago · Santiago Trujillo 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