javascriptreact-nativerealm-js

Object.values on a realmjs object returns an empty array


Description

I'm trying to convert my realm object to an array as can be seen below in the history method.

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
}

When I log this.symptoms I get the following output in my terminal:

{
  '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
  }
}

When I log Object.keys(this.symptoms) I get the following in my terminal:

[ '0', '1', '2', '3' ]

When I log Object.values(this.symptoms) I get the following in my terminal:

[]

This is the only way that this works:

        const values = [];
        for(let prop in this.symptoms){
            if(this.symptoms.hasOwnProperty(prop)){
                values.push(this.symptoms[prop])
            }
        }
        console.log(values);

This logs the following in my terminal:

[{
    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
  }
]

Question:

What is causing the realmjs object to be unable to return an array of values?


Solution

  • Currently unsure as to why Object.values() does not work. I went ahead and used this alternative, which according to some posts may cause performance issues.

    Array.from(this.symptoms);