gpt4 book ai didi

typescript - 如何检查对象 Ionic 4 中的元素

转载 作者:行者123 更新时间:2023-12-04 10:51:51 24 4
gpt4 key购买 nike

我正在使用 Ionic 4。我将数据发布到我的 API,然后我得到如下返回结果:

{
"responses": [
{
"Detection": {
"Images": [
{"url":"https:URL"},
],
"Ages": [
{"age":"33"},
],
"Labels":[
{"label":"game"}
]
}
}
]
}

但有时不返回“年龄”。所以,我的问题是如何检查 API 返回对象中的元素?例如,我想检查响应是否返回 "Ages[]"或不?我该如何写条件?

最佳答案

你可以这样访问;

here is working Example


data={
"responses": [
{
"Detection": {
"Images": [
{"url":"https:URL"},
],
"Ages": [
{"age":"33"},
],
"Labels":[
{"label":"game"}
]
}
}
]
}

this.data.responses[0].Detection.Ages[0].age
console.log("Age :",his.data.responses[0].Detection.Ages[0].age);

//检查年龄礼物
let ages=this.data.responses[0].Detection.Ages

if(ages.length)
{
console.log("Age object present :",ages.length);
}

关于typescript - 如何检查对象 Ionic 4 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59446429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com