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

194
Visualizações
How to get parent key from child in Object

Given an Object, I want to get the parent key starting from the child. Since I only care about the key I'm iterating from enntries, but it always returns undefined.

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 Respostas
Responde à pergunta

0

Simply replace .values() by .keys() in your code:

  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 Relatório

0

here is a solution how to get parent key from children

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 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