我需要使用 vuejs 显示确认消息,如果来自 api.i 的数据在数据字段中包含 true 字段,则在下面给出了 api 输出,以及用于显示警报消息的条件。 在下面的输出中没有 true。每当真实数据出现时,只应显示警报。
//API数据
{
"BNG-JAY-137-003": false,
"BNG-JAY-137-004": false,
"BNG-JAY-137-005": false
}
//下面是我的 if 条件,我将 API 输出存储到可用的“selected_data”
if(this.selected_data){
if(confirm("Do you want to Turn On All lights")){
alert("Lights on")
}
}
最佳答案
您需要循环遍历对象以确定是否有任何属性包含 true
作为值。您可以通过执行以下操作来实现此目的:
let data = {
"BNG-JAY-137-003": false,
"BNG-JAY-137-004": true,
"BNG-JAY-137-005": false
};
let hasTrue = false;
for(let key in data) {
if(data[key]) {
hasTrue = true;
break;
}
}
if(hasTrue) {
if(confirm("Do you want to Turn On All lights")){
alert("Lights on");
}
}
关于javascript - 如果数据为真,则向用户显示确认消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58413380/