Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
Filtrar objeto pasado en propiedades de valor

Tengo un objeto como este:

 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

Quiero devolver verdadero cuando cada clave en el objeto en el que el interruptor está configurado en verdadero no tiene ningún error (ver obj2). (Para machine2, el error '404' no importa, porque el interruptor está configurado en falso).

Intenté algo así, pero no llegué muy lejos.

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

¡Gracias por leer!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

podrías hacer algo como lo siguiente:

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

Entonces verifica si every clave en el objeto tiene el switch configurado en falso o el err es igual a nulo.

about 4 years ago · Juan Pablo Isaza Report

0

Puede hacerlo mediante el uso de entries y every ayudantes de Array , así:

 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 Report

0

¿Quiso decir algo así?

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

Cambié un poco la instalación para hacerlo más fácil.

 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++; }

Resultados:

ERROR 0

ERROR 1

ERROR 0

SUCCES

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!