How to access an object in which the key is a number?

Asked

Viewed 1,531 times

1

I have an object whose key to a property is a number, I can’t access that data using dados.1.TIPOPROCEDIMENTO. How can I do that?

const dados = {
  "1": {
    "TIPOPROCEDIMENTO": "Humanas",
    "DESCRICAO": "Trabalhe%20com%20a%20Ema",
  },
  "2": {
    "TIPOPROCEDIMENTO": "Uniema",
    "DESCRICAO": "Solicita%E7%E3o%20de%20usu%E1rio%20AVE",
  }
}

console.log(dados.1.TIPOPROCEDIMENTO)

1 answer

3

The answer to your problem is quite simple, just change the way you were doing:

dados.1.TIPOPROCEDIMENTO

For:

dados['1']['TIPOPROCEDIMENTO']

See in practice:

const dados = {
  "1": {
    "TIPOPROCEDIMENTO": "Humanas",
    "DESCRICAO": "Trabalhe%20com%20a%20Ema",
  },
  "2": {
    "TIPOPROCEDIMENTO": "Uniema",
    "DESCRICAO": "Solicita%E7%E3o%20de%20usu%E1rio%20AVE",
  }
}

console.log(dados['1']['TIPOPROCEDIMENTO'])

Sample test in React Native (originally asked in the question)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.