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

142
Visualizações
Finding the name of an object

Let's say I have some Javascript with the following:

Foo = {
    alpha: { Name: "Alpha", Description: "Ipso Lorem" },
    bravo: { Name: "Bravo", Description: "Nanu, Nanu" },
    delta: { Name: "Fly with me", Description: "Klaatu barata nikto" }
};

Table = [ Foo.alpha, Foo.bravo, Foo.delta];

x = Table[1];

Is there any way of looking at x and getting the identifier bravo? I'm fully aware that I can use x.Name or x.Description, but let's say that I need to know the name for something elsewhere. In one task I experimented with, I was forced to add a redundant id : "bravo" to each entry, but that was a pain.

My gut tells me it can't be done. But I'm hoping someone else can tell me otherwise.

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

0

Foo = {
    alpha: { Name: "Alpha", Description: "Ipso Lorem" },
    bravo: { Name: "Bravo", Description: "Nanu, Nanu" },
    delta: { Name: "Fly with me", Description: "Klaatu barata nikto" }
};

Table = [ ];
for(let val in Foo){
  let obj = Foo[val];
  obj = {...obj , id:val }
  Table.push(obj)
}
x = Table[1];
console.log(x)

about 4 years ago · Juan Pablo Isaza Relatório

0

Personally, I'd use a Proxy ...

const _Foo = {
    alpha: { Name: "Alpha", Description: "Ipso Lorem" },
    bravo: { Name: "Bravo", Description: "Nanu, Nanu" },
    delta: { Name: "Fly with me", Description: "Klaatu barata nikto" }
};
const Foo = new Proxy(_Foo, {
    get(target, id) {
        if (target.hasOwnProperty(id)) {
           return {...target[id], id};
        }
        return target[id];
    }
});
const Table = [ Foo.alpha, Foo.bravo, Foo.delta ];
console.log(Table[0])

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