Home > OS >  Typescript / Ionic count elements in array returns always 0
Typescript / Ionic count elements in array returns always 0

Time:09-27

I'm coding an Ionic Application, that stores some form entries and some options by using the "storage". Now I would like to count the options, but I'm always getting a zero.

I'm using the following code:

async getAllEntries()
{
let count = 0;
this.trackingEntries = [];
this.storage.forEach((value, key, index) => 
{
  if (key != "Options0" && key != "Options1"  && key != "Options2"  && key != "Options3"  )
  {
    this.trackingEntries.push(value);
    count  ;
  }
})
console.log(this.trackingEntries);  
console.log("Count: "   count);

}

console.log(this.trackingEntries) returns the following object:

[
    {
        "timestamp": "1663677073",
        "tracking": [
            "6",
            "4",
            "5",
            "6"
        ]
    },
    {
        "timestamp": "1663677073",
        "tracking": [
            "2",
            "2",
            "2",
            "2"
        ]
    },
    {
        "timestamp": "1663677073",
        "tracking": [
            "2",
            "2",
            "2",
            "2"
        ]
    }
]

But the count variable always is zero. How can I solve this problem?

CodePudding user response:

I found a solution by using a "then".

async getAllPottyTrackingEntries() {
this.trackingEntries = [];
this.storage.forEach((value, key, index) => {
    if (key != "Options0" && key != "Options1" && key != "Options2" && key != "Options3") {
        this.trackingEntries.push(value);
    }
}).then(() => {
    console.log("Count: "   this.trackingEntries.length)
});

}

  • Related