0
我想在形式我应该如何构建我的数据以便与Firebase一起使用?
wordBank = {
{word:"aprobi", translation:"to approve", count:2},
{word:"bati", translation:"to hit, to beat, to strike", count:1},
{word:"da", translation:"of", count:1}
}
的目标是能够提取并显示在每个JSON对象的所有键的所有值更新一些数据。如何在Firebase上创建此格式?我使用.update?或者是其他东西?
目前我只能得到火力.update()与数组的工作,但它使我的数据是这样
wordBank = [
{word:"aprobi", translation:"to approve", count:2},
{word:"bati", translation:"to hit, to beat, to strike", count:1},
{word:"da", translation:"of", count:1}
];
其中每个字对象是在数组中的索引。
下面是如何构建我的wordObjects:
function getWords() {
if (document.getElementsByClassName("vortarobobelo").length != 0){
var words;
words = document.getElementsByClassName("vortarobobelo")[0].children[0].children;
for (var i =0; i < words.length; i++) {
var localBank = {} //creating the local variable to store the word
var newWord = words[i].children[0].innerText; // getting the word from the DOM
var newTranslation = words[i].children[1].innerText; // getting the translation from the DOM
localBank.word = newWord;
localBank.translation = newTranslation;
localBank.count = 0 //assuming this is the first time the user has clicked on the word
console.log(localBank);
wordBank[localBank.word] = localBank;
fireBank.update(localBank);
}
}
}
用单词存储每个对象作为键听起来像个好主意。但是,当我试图这样做时,而不是更新数据库,它只是覆盖它。 –
使用[Object.assign](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)将旧对象与新对象合并,然后调用'set'返回的值。 –