Get a Value of an Array in JavaScript

1

Greetings to all, I have the following JavaScript code:

var Lista=[];

    $.getJSON("php/consultas/TraerDatos.php", function( data ) {
       $.each(data, function(id,value){
           var elemento = {
               'Proyecto':''+value['Proyecto'] +'',
               'Motivo':''+value['Motivo']+'' ,
               'Codigo': ''+value['Codigo']+'' ,
               'otro': ''+value['Motivo']+'' ,
               'Lugar': ''+value['Lugar']+''
       };

       Lista.push(elemento);
       });
   });

And I need to take the value 'Code' from the array 'List' to assign it to another variable

var temp = /* Asignación que no tengo clara hacer del objeto del array 
           Lista de su valor 'codigo' en la posición 0 */

only that value, so I guess I do not need to go through it with foreach, in the context of my function all these elements have the same value in the code, I would get it from this array in position 0, I have tried but not me works. What is the correct way to obtain this value in this case?

As always I am open to your suggestions and advice. Thanks for your time.

    
asked by Gutierrez 22.11.2016 в 17:23
source

1 answer

4

Do you only want to retrieve the value of the Codigo attribute of the first item in the list? That's how it is:

var temp = Lista.length ? Lista[0].Codigo : null;

Now, if you just want that, and you're not going to do anything with the rest of the values in the list, you could also simplify and do this:

var codigo = null;
$.getJSON("php/consultas/TraerDatos.php", function(data) {
  if (data.length) {
    codigo = data[0].Codigo
  }
});
    
answered by 22.11.2016 / 17:31
source