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

126
Views
No obtener todas las claves del objeto anidado usando Javascript

Estoy tratando de obtener todas las claves de un objeto anidado usando Javascript pero según mi código no funciona como se esperaba. Estoy explicando mi código a continuación.

 let jsonObj = { "service":[ { "name":"restservice", "device":"xr-1", "interface-port":"0/0/2/3", "interface-description":"uBot testing for NSO REST", "addr":"10.10.1.3/24", "mtu":1024 } ], "person": { "male": { "name": "infinitbility" }, "female": { "name": "aguidehub" } } } const getNestedKeys = (data, keys) => { if(!(data instanceof Array) && typeof data == 'object'){ Object.keys(data).forEach(key => { keys.push(key); const value = data[key]; if(typeof value === 'object' && !(value instanceof Array)){ getNestedKeys(value, keys); } }); } return keys } let result = getNestedKeys(jsonObj, []); console.log('result', result);

Aquí no obtengo las claves presentes dentro de la matriz de service . Necesito buscar todas las claves presentes dentro de un objeto anidado usando Javascript.

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

0

Puedes hacerlo

 const readAllKeys = obj => { if(typeof obj !== 'object'){ return [] } if(Array.isArray(obj)){ return obj.flatMap(readAllKeys) } return [...Object.keys(obj), ...Object.values(obj).flatMap(readAllKeys)] } let jsonObj = { "service":[ { "name":"restservice", "device":"xr-1", "interface-port":"0/0/2/3", "interface-description":"uBot testing for NSO REST", "addr":"10.10.1.3/24", "mtu":1024 } ], "person": { "male": { "name": "infinitbility" }, "female": { "name": "aguidehub" } } } console.log(readAllKeys(jsonObj))

about 4 years ago · Juan Pablo Isaza Report

0

Está solicitando que funcione con la clave de "servicio", que es una matriz, pero su código verifica explícitamente el tipo y, si es una matriz, simplemente la ignora. Aquí está el código ajustado para trabajar con matriz también.

 let jsonObj = { "service":[ { "name":"restservice", "device":"xr-1", "interface-port":"0/0/2/3", "interface-description":"uBot testing for NSO REST", "addr":"10.10.1.3/24", "mtu":1024 } ], "person": { "male": { "name": "infinitbility" }, "female": { "name": "aguidehub" } } } const getNestedKeys = (data, keys) => { if(data instanceof Array) { data.forEach(value => { getNestedKeys (value, keys); }); } if(!(data instanceof Array) && typeof data == 'object'){ Object.keys(data).forEach(key => { keys.push(key); const value = data[key]; if(typeof value === 'object'){ getNestedKeys(value, keys); } }); } return keys } let result = getNestedKeys(jsonObj, []); console.log('result', result);

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!