javascript - 如何使用打字稿来获取对象数组的长度
问题描述
我试图通过使用 API 调用从后端获取对象数组的长度。这是“区域”目标的样子:
[
{
"id": 1,
"name": "a"
},
{
"id": 2,
"name": "b"
},
{
"id": 3,
"name": "c"
},
{
"id": 4,
"name": "d"
}
]
并且以下打字稿代码未正确返回长度:
var lengthZones= Object.keys(this.swimbandsService.getZones()).length;
在打字稿中获取对象数组长度的正确方法是什么?
解决方案
我猜您需要先等待 Promise 解决,然后您才能访问响应并计算数组中的对象。
this.swimbandsService.getZones().then((response) => {
// might need to parse the response as json, depending on your service logic
var lengthZones = response.length;
});
推荐阅读
- sql - 使用子查询加入 2 个外键
- javascript - 向 AdonisJS 中的所有模型添加自定义函数
- undefined - 寻找未定义的符号:_ZN5boost11this_thread6hidden9sleep_forERK8timespec
- angular - 如何仅重置角度5中的特定表单字段
- reactjs - 尝试运行构建时出错
- c# - ASP.Net Core Razor Pages 动态列表
- java - 无法与 Instagram 登录字段交互
- php - 如何根据 PHP 表格列中的某些条件更改文本颜色
- java - FopFactory 类型中的方法 newInstance(FopFactoryConfig) 不适用于参数 ()
- reactjs - 使用参数反应路由器链接