【问题标题】:javascript understanding if else statementjavascript理解if else语句
【发布时间】:2020-06-13 18:26:28
【问题描述】:

嘿,我今天刚开始学习 javascript。

而且我在理解 else if 语句时遇到了一些麻烦。 ​

我想如果第一个 if 语句为真,那么其他两个 else if 语句就不会被调用? ​

如果第一个 if 语句为 false,它会尝试第二个 else if 语句,如果第二个返回 false,它会尝试第三个 else if 语句?

但是所有的陈述都是真实的,我不知道为什么,希望有人能向我解释为什么? ​

  var Builds = [
    { dead: 0, tier: 8, type: "Stash", uid: 9989, x: 4032, y: 11424 },
    { dead: 0, tier: 1, type: "Wall", uid: 9990, x: 4042, y: 11525 },
    { dead: 0, tier: 1, type: "Wall", uid: 9991, x: 4052, y: 11526 },
    { dead: 0, tier: 0, type: "Wall", uid: 9992, x: 4062, y: 11527 },
    { dead: 0, tier: 0, type: "Door", uid: 9993, x: 4072, y: 11428 },
    { dead: 0, tier: 0, type: "Door", uid: 9994, x: 4082, y: 11429 },
    { dead: 0, tier: 8, type: "Door", uid: 9995, x: 4092, y: 11430 },
    { dead: 0, tier: 1, type: "Gun", uid: 9996, x: 4100, y: 11431 }
  ];

  Object.keys(Builds).filter(function(e) {
    return ("Stash" == Builds[e].type || "Wall" == Builds[e].type || "Door" == Builds[e].type);
  }).forEach(s => {

    if (Builds[s].type == "Stash") {
      console.log(Builds[s].type)

    } else if (Builds[s].type == "Wall") {
      console.log(Builds[s].type)

    } else if (Builds[s].type == "Door") {
      console.log(Builds[s].type)

    }
  })

对不起,如果我的英语不好。

【问题讨论】:

    标签: javascript


    【解决方案1】:

    您对 if-else 的描述是正确的。它会全部尝试,直到其中一个为真,然后执行相应的代码块,而不检查后面的代码块。

    但是,您为Builds 数组中与过滤器匹配的每个条目执行一次 if-else 级联。

    【讨论】:

      【解决方案2】:

      您的 if else 块是正确的,并且它正在为从过滤器函数返回的每个数组实例运行。 Filter 方法返回数组中匹配值的所有索引,并且您在 if else 块中检查 Builds 数组中的相同索引值。所以它从 if else 块中返回所有匹配的条件。

      【讨论】:

        【解决方案3】:

        您不需要获取 Object.keys,因为 Builds 是一个数组。您可以直接在数组上使用过滤器,其中过滤器返回数组中的每个项目。请参阅下面的代码。

        就像 Joachim 在您的回答中所说,您正在过滤除 gun 之外的所有内容。 filter 方法返回一个新的(过滤的)数组,然后您使用 for each 循环遍历该数组。

        我将您的代码放在一个函数中,并同时发送Build 数组和具有不同过滤器的数组,以展示如何以更动态的方式使用过滤器方法。

        var Builds = [
            { dead: 0, tier: 8, type: "Stash", uid: 9989, x: 4032, y: 11424 },
            { dead: 0, tier: 1, type: "Wall", uid: 9990, x: 4042, y: 11525 },
            { dead: 0, tier: 1, type: "Wall", uid: 9991, x: 4052, y: 11526 },
            { dead: 0, tier: 0, type: "Wall", uid: 9992, x: 4062, y: 11527 },
            { dead: 0, tier: 0, type: "Door", uid: 9993, x: 4072, y: 11428 },
            { dead: 0, tier: 0, type: "Door", uid: 9994, x: 4082, y: 11429 },
            { dead: 0, tier: 8, type: "Door", uid: 9995, x: 4092, y: 11430 },
            { dead: 0, tier: 1, type: "Gun", uid: 9996, x: 4100, y: 11431 }
          ];
        
        function filterBuilds(buildsArr, filterArr) {
          buildsArr.filter((build) => {
            return filterArr.includes(build.type);
          })
          .forEach((build) => {
            if (build.type == "Stash") {
              console.log(build.type);
            } else if (build.type == "Wall") {
              console.log(build.type);
            } else if (build.type == "Door") {
              console.log(build.type);
            }
          })
        }
        
        filterBuilds(Builds, ['Stash', 'Wall']);
        console.log('-----');
        filterBuilds(Builds, ['Stash']);

        【讨论】:

          【解决方案4】:

          根据您的 sn-p,您正在过滤 Builds Array 以获取 stash、wall 和 Door 类型。 过滤的内容被推送到应用 if else 语句的新数组,新数组中每个条目的第二次迭代返回 true,因为它们与过滤器匹配,但最后一次迭代返回 Undefined

          【讨论】:

            猜你喜欢
            • 2021-09-11
            • 1970-01-01
            • 2016-06-21
            • 2015-05-16
            • 1970-01-01
            • 2021-11-17
            • 1970-01-01
            • 1970-01-01
            • 2019-04-09
            相关资源
            最近更新 更多