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

标签 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: 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 上找到的每个示例都很复杂且支离 splinter 。感谢您的帮助!

var database = firebase.database();
var i;
var j;
for (i = 0; i < 7; i++) {
    for (j = 0; j < 6; j++) {
	var R = i.toString();
	var C = j.toString();
	var id = C.concat("",R);
	var col = "c"+R
	var x = piece(i,j); 
    document.getElementById(id).innerHTML = x;
	}
	function piece(i,j) {
	var C = j.toString();
	var col = "c"+C
		return database.ref('board1/'col).once('value').then(function(snapshot) {
		  console.log(snapshot.val());
		  var piece = snapshot.val()[i];
		  console.log(piece);
		});
	});      
}
}

最佳答案

如果您想使用实时数据库,您可以执行以下操作。只需将其复制粘贴到 html 页面中,根据您的项目调整配置值,然后使用浏览器打开它。

打开时,它将把c0数组写入board1节点。然后通过控制台手动修改C03的值,点击“读取数据”按钮。

所以这是一个解决方案。但是,您应该阅读这篇 Firebase 博客文章:https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html

<html>

<head>
  <script src="https://www.gstatic.com/firebasejs/5.0.1/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/5.0.1/firebase-database.js"></script>
</head>

<body>

<button id="myBtn">Read data</button>

<script>
  // Initialize Firebase
  var config = {
    apiKey: "",
    authDomain: "",
    databaseURL: "",
    projectId: ""
  };
  firebase.initializeApp(config);

  var db = firebase.database();

  var newQuoteKey = db.ref('board1/').push().key;

  var updates = {};
  updates['c0'] = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0};

  db.ref('board1/').update(updates);


  document.getElementById("myBtn").addEventListener("click", function(){
    return db.ref('board1/c0').once('value').then(function(snapshot) {
      console.log(snapshot.val());
      var c03 = snapshot.val()[3];
      console.log(c03);
    });
  });

</script>

<body>
</html>
<小时/>

编辑问题(添加代码)并在下面添加评论后进行编辑

我将重构您的代码如下(未经测试,只是“高级”重构,没有所有细节):

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
    }
})

关于javascript - Firebase 使用 JavaScript 提取数组中的特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50359339/

相关文章:

javascript - 如何在类组件中定位 Prop

python - 如何调用 json 字典中列表中的值

json - 通过 json 查询将图片上传到 BigCommerce

firebase - Firestore.settings() 要求它的第一个参数是对象类型,但它是一个自定义对象

javascript - 主干事件不会在添加到 DOM 的新元素上触发

jquery - 使用 jquery 合并两个 json 对象

node.js - Angular 在 firestore 中添加新文档而无需列表

javascript - 如何从文件中导出多个对象

javascript - JSON.parse 中 JSON 中位置 0 处出现意外标记 <

javascript - 查询选择器全部 : manipulating nodes