首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何继续if函数并记录所有结果

如何继续if函数并记录所有结果
EN

Stack Overflow用户
提问于 2021-10-23 03:15:59
回答 2查看 47关注 0票数 1

我有一个关于If函数的问题,如果条件1为真,我想继续条件2,依此类推,最后将记录所有的结果,但目前只有在条件1为真的情况下才不会继续,这是代码:

代码语言:javascript
运行
复制
       if (data1[0].themes[0].title) {
            message.channel.send(data1[0].themes[0].title)
            console.log(`1 sent`)
        } else if (data1[0].themes[1].title) {
            message.channel.send(data1[0].themes[1].title)
            console.log(`2 sent`)
        } else if (data1[0].themes[2].title) {
            message.channel.send(data1[0].themes[2].title)
            console.log(`3 sent`)
        }
         else message.channel.send(`no result`)
EN

回答 2

Stack Overflow用户

发布于 2021-10-23 03:34:59

这就是你想要实现的吗?我假设'title‘属性包含一些布尔值( if语句中的条件需要一个true或false值)。这将检查每个条件并发送消息。如果没有发送数据,它将在通道中广播“no result”。

代码语言:javascript
运行
复制
let sent = false
if (data1[0].themes[0].title) {
    message.channel.send(data1[0].themes[0].title)
    sent = true
    console.log(`1 sent`)
}
if (data1[0].themes[1].title) {
    message.channel.send(data1[0].themes[1].title)
    sent = true
    console.log(`2 sent`)
}
if (data1[0].themes[2].title) {
    message.channel.send(data1[0].themes[2].title)
    sent = true
    console.log(`3 sent`)
}
if (!sent) {
    message.channel.send(`no result`)
}
票数 3
EN

Stack Overflow用户

发布于 2021-10-23 03:23:34

它是条件分支,您应该将条件2移动到condition1内部

代码语言:javascript
运行
复制
if(condition1) {
  if (condition2) {
  } else if (conditio3) {
  } else ...
} else {
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69684838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档