How can I read a key that is formed by id _ name in javascript?

1

I try to read a variable from a json. But this variable has the following syntax.

"id_nombre":{
Variables,
}

Looking at a serious example:

0_seccion:{
variables
}

My problem is that I do not know how to access this json. already try with:

objeto.id_nombre
objeto.'id_nombre'
objeto['id_nombre]

But in all it fails.

That's how I got the json back. It must have this format since the id identifies the section and names the component. And if this nomenclature is not followed, there may be a component with the same name.

What I want to do is:

var otra = variable.0-MenuComponent;
    
asked by Jorge Alberto Ortega Ceja 27.09.2018 в 23:00
source

3 answers

1

That's how I got the json back. It must have this format since the id identifies the section and names the component. And if this nomenclature is not followed, there may be a component with the same name.

What I want to do is:

var otra = variable.0-MenuComponent;
    
answered by 27.09.2018 в 23:27
1

The names of nodes can be àfameric.

variable.some , variable.algoLocochon10

When you need characters like "-" and mix it with variables.

variable['nombre-con-guion']

Or if you want

var numero = 0;

variable[numero + '-nombre'];

If you use the ES6 version

var numero = 1;

variable['${numero}-nombre']
    
answered by 28.09.2018 в 01:23
1

The first one is not valid because you can not store an object in a string , anyway you are creating a dictionary and you are not assigning a value to a key.

Not valid:

"id_nombre":{
    Variables,
}

Valid:

var id_nombre = {
  variable1:" algo",
  "nombre":" otro",
}
alert(id_nombre.variable1);
alert(id_nombre["nombre"]);

And the second example is not valid since a variable should not start with a number.

    
answered by 27.09.2018 в 23:21