你能帮我解决这个问题吗?
export class SomePage {
reg = [{apple: 201, ball: 21, cat: 12}, {apple: 202, ball: 65, cat: 15}];
constructor() {
firebase.database().ref('app/test').set({
// i want to set all ***cat*** values here like:
// key1: 12
// key2: 15
})
尝试使用此方法提取cat
值:
let keys = this.reg.map(a => a.cat);
console.log(keys); // <unavailable>
答案 0 :(得分:0)
//1
var reg = [{apple: 201, ball: 21, cat: 12}, {apple: 202, ball: 65, cat: 15}];
//2
var resultObject = {};
//3
var count = 0;
//4
reg.filter(function(elem){ resultObject['key'+ ++count] = elem.cat});
//5
constructor() {
firebase.database().ref('app/test').set(resultObject);
}
答案 1 :(得分:0)
找出解决方案:
this.reg.map(a => {
let first = a.ball; // or add any key
let addORupdate = {};
addORupdate[first] = a.cat; // expected value from array
firebase.database().ref('app/test').update(addORupdate);
// this will add 21:12 and 65:15 under the parent "test"
});