描述
我正在尝试将我的领域对象转换为数组,如下面的 history 方法所示。
class RealmStore {
@observable symptoms = {};
@observable meals = {};
@computed get history(){
return [...Object.values(this.symptoms), ...Object.values(this.meals)];
}
//More methods to populate this.symptoms and this.meals
}
当我登录时this.symptoms
,我在终端中得到以下输出:
{
'0': {
date: Fri Jun 29 2018 15: 56: 48 GMT + 0200(CEST),
name: 'Regurgitation',
value: 1
},
'1': {
date: Fri Jun 29 2018 15: 58: 09 GMT + 0200(CEST),
name: 'Belching',
value: 1
},
'2': {
date: Fri Jun 29 2018 16: 10: 39 GMT + 0200(CEST),
name: 'Heartburn',
value: 2
},
'3': {
date: Fri Jun 29 2018 23: 30: 36 GMT + 0200(CEST),
name: 'Heartburn',
value: 1
}
}
当我登录时Object.keys(this.symptoms)
,我在终端中得到以下信息:
[ '0', '1', '2', '3' ]
当我登录时Object.values(this.symptoms)
,我在终端中得到以下信息:
[]
这是唯一可行的方法:
const values = [];
for(let prop in this.symptoms){
if(this.symptoms.hasOwnProperty(prop)){
values.push(this.symptoms[prop])
}
}
console.log(values);
这会在我的终端中记录以下内容:
[{
date: Fri Jun 29 2018 15: 56: 48 GMT + 0200(CEST),
name: 'Regurgitation',
value: 1
},
{
date: Fri Jun 29 2018 15: 58: 09 GMT + 0200(CEST),
name: 'Belching',
value: 1
},
{
date: Fri Jun 29 2018 16: 10: 39 GMT + 0200(CEST),
name: 'Heartburn',
value: 2
},
{
date: Fri Jun 29 2018 23: 30: 36 GMT + 0200(CEST),
name: 'Heartburn',
value: 1
}
]
问题:
是什么导致 realmjs 对象无法返回值数组?