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

255
Visualizações
the way of accessing the number element of object in javascript in '.' causes an error

First, I am a beginner of javascript. Please understand asking this simple question.. (looks simple)

I was making a membership related webpage and encountered an error.. I was learned that to access the element of an object, I can use '.'(dot) so I coded like, It was a bit long object(json type contents) but.. to sum up,

dict1 = {'a':1, 'b':2, 'c':3};

dict2 = {1:'a', 2:'b', 3:'c'}

dict1.a result: 1

dict2.1 result: Uncaught SyntaxError: Unexpected number

so the point is, Someone may think, "if you can see the elements, why would you access the element by '.'(dot). if you already know it can cause the error." but the data is user input and users can input any data as they wish.

Javascript provides '.'(dot) operator for Object but not working for number Element??

or do I use the dot in wrong way?

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

0

If you use dot notation, your key must be a valid identifier (start with a letter, $ or _). So in this case you would need to use dict2['1'].

about 4 years ago · Juan Pablo Isaza Relatório

0

You can do something like this to get the values

// With the braces you can get the values even if the key is a number
let dict2 = {
  1: 'a',
  2: 'b',
  3: 'c'
}

console.log(dict2[1])

// Even if you want to access the values that are having keys like strings you can use the same operator
dict1 = {'a':1, 'b':2, 'c':3};

console.log(dict1['a'])

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