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

4smxwvx5  于 2022-12-08  发布在  Ionic
关注(0)|答案(1)|浏览(132)

我正在编写一个Ionic应用程序,它通过使用“存储”来存储一些表单条目和一些选项。现在我想计算选项,但我总是得到零。
下面的代码是:

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)会传回下列对象:

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

但是计数变量总是零,我该怎么解决这个问题呢?

0dxa2lsx

0dxa2lsx1#

我用“那么”找到了解决办法。

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)
});

}

相关问题