如何在javascript中重新构造firebase数据
鉴于以下情况,如何重新构造数据,使标记结构如下所示如何在javascript中重新构造firebase数据,javascript,firebase,ionic-framework,firebase-realtime-database,Javascript,Firebase,Ionic Framework,Firebase Realtime Database,鉴于以下情况,如何重新构造数据,使标记结构如下所示 "tags" : { "Tag3": true, "Tag4": true }, 目前我的代码将数据推送到firebase,如下所示: "-KXcnfob3Vo3s8bL9WSI" : { "name" : "Product 2", "description" : "Description of product 2", "tags" : [ "Tag 1", "Tag 2", "Tag 3", "e
"tags" : {
"Tag3": true,
"Tag4": true
},
目前我的代码将数据推送到firebase,如下所示:
"-KXcnfob3Vo3s8bL9WSI" : {
"name" : "Product 2",
"description" : "Description of product 2",
"tags" : [ "Tag 1", "Tag 2", "Tag 3", "etc." ],
"url" : "websiteURL2.com",
}
"-KXcnfob3Vo3s8bL9WSI" : {
"name" : "Product 2",
"description" : "Description of product 2",
"tags" : {
"Tag3": true,
"Tag4": true
},
"url" : "websiteURL2.com"
}
我希望这样做:
"-KXcnfob3Vo3s8bL9WSI" : {
"name" : "Product 2",
"description" : "Description of product 2",
"tags" : [ "Tag 1", "Tag 2", "Tag 3", "etc." ],
"url" : "websiteURL2.com",
}
"-KXcnfob3Vo3s8bL9WSI" : {
"name" : "Product 2",
"description" : "Description of product 2",
"tags" : {
"Tag3": true,
"Tag4": true
},
"url" : "websiteURL2.com"
}
以下是相关代码:
firebase.database().ref().child('products').push({
name: $scope.product.name,
description: $scope.product.description,
url: $scope.product.url,
dateCreated: Date(),
tags: $scope.productTags
}
$scope.addToTag = function(tag) {
if($scope.productTags.indexOf(tag) == -1)
$scope.productTags.push(tag);
};
提前谢谢 像这样更改addToTag方法 $scope.addToTag=函数(标记){ }) 确保$scope.productTags是一个对象
var-productTags={};
var addToTag=函数(标记){
productTags[tag]=true;
};
addToTag(“Tag1”);
console.log(productTags)代码>像手套一样合身!!非常感谢!!