Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
Cómo obtener la clave principal del niño en Object

Dado un objeto, quiero obtener la clave principal a partir del elemento secundario. Como solo me importa la clave, estoy iterando desde las entradas, pero siempre devuelve indefinido.

 var book = { "chapter1": { "paragraph1": { "text": "..." }, "paragraph2": { "text": "..." } }, "chapter2": { "paragraph3": { "text": "..." }, "paragraph4": { "text": "..." } }, "chapter3": { "paragraph5": { "text": "..." }, "paragraph6": { "text": "..." } } }; var section = "paragraph3"; const category = Object.entries(book).find(([, e]) => Object.values(e).includes(section)); /// should return chapter2 if (category) { console.log(category[0], category[1]); } else { console.log("Not Found"); }

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Simplemente reemplace .values() por .keys() en su código:

 var book = { "chapter1": { "paragraph1": { "text": "..." }, "paragraph2": { "text": "..." } }, "chapter2": { "paragraph3": { "text": "..." }, "paragraph4": { "text": "..." } }, "chapter3": { "paragraph5": { "text": "..." }, "paragraph6": { "text": "..." } } }; var section = "paragraph3"; const category = Object.entries(book).find(([, e]) => Object.keys(e).includes(section)); /// should return chapter2 if (category) { console.log(category[0], category[1]); } else { console.log("Not Found"); }

about 4 years ago · Juan Pablo Isaza Denunciar

0

aquí hay una solución sobre cómo obtener la clave principal de los niños

 var book = { chapter1: { paragraph1: { text: '...' }, paragraph2: { text: '...' } }, chapter2: { paragraph3: { text: '...' }, paragraph4: { text: '...' } }, chapter3: { paragraph5: { text: '...' }, paragraph6: { text: '...' } } }; var section = 'paragraph3'; const category = Object.entries(book).find(e => Object.keys(e[1]).includes(section) ); /// should return chapter2 if (category) { console.log(category[0]); } else { console.log('Not Found'); }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda