鉴于以下内容,我如何重构我的数据,以便我的标签结构如下所示?
"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"
}
以下是相关代码:
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);
};
提前致谢!
答案 0 :(得分:1)
像这样改变你的addToTag方法。
$ scope.addToTag = function(tag){
$scope.productTags[tag]=true;
};
确保$ scope.productTags是一个对象。
var productTags = {};
var addToTag = function(tag) {
productTags[tag]= true;
};
addToTag("Tag1");
console.log(productTags);