js中在tx.executeSql内赋值的数组跳出函数后不能按下标查找数组,为什么?

js中在tx.executeSql内赋值的数组跳出函数后不能按下标查找数组,为什么?
代码如下:

//JavaScript Document
$(function ($) {
    var array=[];
    var db = openDatabase('mydb', '1.0', 'Test DB', 2*1024*1024);

    $(function ($){
        init();
        show();
    });
    function init(){
      db.transaction(function (tx) {  
        tx.executeSql('CREATE TABLE IF NOT EXISTS USER (id, username, sex, age, phone, address )');
        tx.executeSql('INSERT INTO USER (id, username, sex, age, phone, address ) VALUES (?, ?, ?, ?, ?, ?)',[1, '李明', '男', 12, 13850501234, '福建']);
        tx.executeSql('INSERT INTO USER (id, username, sex, age, phone, address ) VALUES (?, ?, ?, ?, ?, ?)',[2, '周福', '男', 23, 13850502345, '上海']);
      });
    }
    function show(){
      db.transaction(function (tx){
          tx.executeSql('select * from USER',[],function (tx,results){
          var len = results.rows.length;
          var i;
          for(i=0;i<len;i++){
              let object;
              object = results.rows.item(i);
              array[i]={id:object.id,username:object.username,sex:object.sex,age:object.age,phone:object.phone,address:object.address};
            }
          **console.log(array[1]);**
            });
          **console.log(array[1]);**
        });
    }

}

运行结果:
usermanager.js:30 undefined
usermanager.js:28 {id: 2, username: "周福", sex: "男", age: 23, phone: 13850502345, …}
图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐