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

131
Visualizações
Filter Object passed on Value Properties

I have an object like this:

let obj = {'machine1':{'err':'404', switch:false},
           'gadget1':{'err':'404', switch:true}}

Would not pass validation. => return false



let obj2 = {'machine2':{'err':'404', switch:false},
           'gadget2':{'err':null, switch:true}}

Would pass validation. => return true

I want to return true when every key in the object in which the switch is set to true does not have any errors (see obj2). (For machine2 the '404' error does not matter, because the switch is set to false.)

I tried something like that, but didn't get far.

function try() { 
  for (x in obj) {
    obj[x].filter(x=>y.switch === true)
}
}

Thanks for reading!

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

0

you could do something like the follow:

const test = obj => Object.keys(obj).every(k => !obj[k].switch || obj[k].err === null)

So you check if every key in the object has switch set to false or the err equal to null.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do it by usign entries and every helpers of Array, like this:

let obj = {
  'machine1': {
    'err': '404',
    switch: false
  },
  'gadget1': {
    'err': '404',
    switch: true
  }
}

let obj2 = {
  'machine2': {
    'err': '404',
    switch: false
  },
  'gadget2': {
    'err': null,
    switch: true
  }
};
const validate = (data) => {
  return Object.entries(data).every(([key, value]) => !value.switch || value.err === null)
}

console.log(validate(obj));
console.log(validate(obj2));

about 4 years ago · Juan Pablo Isaza Relatório

0

Did you mean something like that?

let obj={
    1:{
        'machine':{'err':'404', switch:false},
        'gadget':{'err':'404', switch:true}
    },
    2:{
        'machine':{'err':'404', switch:false},
        'gadget':{'err':null, switch:true}
    }
}

I changed the facility a bit to make it easier.

var n=1;
while (n<=2) {

if(obj[n].machine.switch==true){
    if(obj[n].machine.err!='404')
       console.log('SUKCES')
    else console.log('ERROR 1')
}
else console.log('ERROR 0')

if(obj[n].gadget.switch==true){
    if(obj[n].gadget.err!='404')
       console.log('SUKCES')
    else console.log('ERROR 1')
}
else console.log('ERROR 0')
n++;
}

Results:

ERROR 0

ERROR 1

ERROR 0

SUKCES

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