Call an element of an array whose element name is in a variable

0

Suppose we have the following:

in data I have the values title, first names, last name

and I have an array with the following elements

person [0]. title, person [0] .names, person [0]. lastname

I use a for as follows:

for (var a in data) {

}

How can I use the value from to to refer to the array element whose name is contained in to

Thank you very much, I appreciate your help in advance

    
asked by Pillo 10.08.2017 в 23:52
source

2 answers

1

In the cycle you use to iterate, you can access each of the attributes that the element has:

for (var a in data) {
  a.titulo;
  a.nombres;
  a.apellido;
}

The variable a takes the element of the vector on which it is iterated, therefore it behaves like the object in question [Person (title, names, surname)]

    
answered by 11.08.2017 в 00:19
0
for (var a in data) 
{
   console.log('Título: '+persona[a].titulo+ ', Nombre: '+persona[a].nombres+', persona[a].apellido');
}
    
answered by 11.08.2017 в 00:25