简体   繁体   English

Typescript / 数组中的离子计数元素始终返回 0

[英]Typescript / Ionic count elements in array returns always 0

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: console.log(this.trackingEntries) 返回以下 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?我怎么解决这个问题?

I found a solution by using a "then".我通过使用“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)
});

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM