suitable function to access array values

0

I have a file with a function that takes all the records of a table, as below.

window.lerTabelaListas=function()
    {
       lista=new Array();
        db.transaction(function  (tx)
        {

         tx.executeSql('SELECT * FROM listas',[],function  (tx, results)
         {

             var len= results.rows.length, i;
             for(i=0; i<len; i++)
             {
               var nome=results.rows.item(i).nome
               var id=results.rows.item(i).idLista
            lista.push(nome,id)


             }


         });

    }); 
   return lista;
   }

As I call this function I can not get any return, I can not, for example, make a foreach to extract these values. By console , however, it returns:

Array(32)
0
:
"lista de: 2/3/2018"
1
:
1
2
:
"lista básica "

I need some help, as I'm a beginner in JavaScript .

    
asked by anonymous 04.03.2018 / 20:25

1 answer

0

Create a global variable out of function:

var resultado = window.lerTabelaListas;
console.log(resultado);

// A sua função
window.lerTabelaListas=function()
{
   lista=new Array();
    db.transaction(function  (tx)
    {

     tx.executeSql('SELECT * FROM listas',[],function  (tx, results)
     {

         var len= results.rows.length, i;
         for(i=0; i<len; i++)
         {
           var nome=results.rows.item(i).nome
           var id=results.rows.item(i).idLista
        lista.push(nome,id)


         }


     });

}); 
return lista;
}
    
05.03.2018 / 18:18