Firebase使用JavaScript提取数组中的特定项

Firebase使用JavaScript提取数组中的特定项,javascript,json,firebase,firebase-realtime-database,Javascript,Json,Firebase,Firebase Realtime Database,我正在尝试创建一个简单的表,该表由我在FireBase中创建的表填充。我是FireBase的新手,我通常在mySQL中工作,并使用php和SQL的组合从数据库中提取数据。我当前的JSON结构是{ 董事会1:{ c0:0:0,1:0,2:0,3:0,4:0,5:0,6:0, c1:0:0,1:0,2:0,3:0,4:0,5:0,6:0, c2:0:0,1:0,2:0,3:0,4:0,5:0,6:0, c3:0:0,1:0,2:0,3:0,4:0,5:0,6:0, c4:0:0,1:0,2:0,3:

我正在尝试创建一个简单的表,该表由我在FireBase中创建的表填充。我是FireBase的新手,我通常在mySQL中工作,并使用php和SQL的组合从数据库中提取数据。我当前的JSON结构是{ 董事会1:{ c0:0:0,1:0,2:0,3:0,4:0,5:0,6:0, c1:0:0,1:0,2:0,3:0,4:0,5:0,6:0, c2:0:0,1:0,2:0,3:0,4:0,5:0,6:0, c3:0:0,1:0,2:0,3:0,4:0,5:0,6:0, c4:0:0,1:0,2:0,3:0,4:0,5:0,6:0, c5:0:0,1:0,2:0,3:0,4:0,5:0,6:0, c6:0:0,1:0,2:0,3:0,4:0,5:0,6:0, c7:0:0,1:0,2:0,3:0,4:0,5:0,6:0 } } 我正在寻找一种方法来检索特定单元格的值,例如c3和数组中的第三项,并将其存储为javascript变量。我在FireBase上找到的每一个例子都是复杂的、不完整的。谢谢你的帮助

var database=firebase.database; var i; var j; 对于i=0;i<7;i++{ 对于j=0;j<6;j++{ var R=i.toString; var C=j.toString; var id=C.concat,R; var col=c+R var x=分段i,j; document.getElementByIdid.innerHTML=x; } 函数i,j{ var C=j.toString; var col=c+c 返回database.ref'board1/'col.once'value'。然后返回FunctionSnapshot{ console.logsnapshot.val; var piece=snapshot.val[i]; 控制台、日志; }; }; }
} 如果您想使用实时数据库,可以执行以下操作。只需将其复制粘贴到html页面中,将配置值与项目中的值相适应,然后用浏览器打开即可

打开时,它会将c0阵列写入board1节点。然后,通过控制台手动修改C03的值,并单击读取数据按钮

所以这是一个解决方案。但是,您应该阅读以下Firebase博客文章:

编辑问题代码并在下面添加注释后进行编辑

我将按照以下方式重构您的代码:未经测试,只是一个高级重构,没有所有细节:

var database = firebase.database();
var i;
var j;

var promises = [];

for (i = 0; i < 7; i++) {
    for (j = 0; j < 6; j++) {
    var C = j.toString();
    var col = "c" + C;

    promises.push(return database.ref('board1/' + col).once('value'));    

    }    
}

var promiseAll = Promise.all(promises);
promiseAll.then(function(results) {
    //Here, result items in the results array
    //are ordered in the same order they were pushed to the promises array 
    for (index = 0; index < results.length; ++index) {
       console.log(results[index]);
       //Do something with the data
       //You will have to write the correct code to find back the i,j couples
    }
})

你能从更高的角度详细说明你的要求吗?你从哪里得到这些数据?它是否已经格式化为那样,或者您格式化为那样,例如legacy。您计划使用哪个数据库?实时数据库或Firestore?嗨,雷诺,我已经在我的代码中添加了我实现您的解决方案的尝试。我没有得到任何填充表的内容。请尝试返回database.ref'board1/'+col.once'value'。然后返回FunctionSnapshot{在更新代码中。传递给ref方法的paralmeter应该是一个字符串。此外,在更新代码中,请注意,once是一个异步函数,返回一个PROMITE see。因此,像您这样声明循环中的所有调用可能会产生一些问题或不一致。最好使用PROMITE.all。
var database = firebase.database();
var i;
var j;

var promises = [];

for (i = 0; i < 7; i++) {
    for (j = 0; j < 6; j++) {
    var C = j.toString();
    var col = "c" + C;

    promises.push(return database.ref('board1/' + col).once('value'));    

    }    
}

var promiseAll = Promise.all(promises);
promiseAll.then(function(results) {
    //Here, result items in the results array
    //are ordered in the same order they were pushed to the promises array 
    for (index = 0; index < results.length; ++index) {
       console.log(results[index]);
       //Do something with the data
       //You will have to write the correct code to find back the i,j couples
    }
})