【问题标题】:Avoid else statement running multiple times in a FOR loop?避免在 FOR 循环中多次运行 else 语句?
【发布时间】:2017-05-12 20:22:39
【问题描述】:

我有这个数组,我怎样才能避免 else 语句发生与数组中的项目一样多的次数

             SoftwareBadges =
            [
{ Title: "Playtech", Guid: "7e9", xPos: "96" },
{ Title: "BetSoft", Guid: "890", xPos: "169" },
{ Title: "WagerWorks", Guid: "35c", xPos: "242" },
{ Title: "Rival", Guid: "c35", xPos: "314" },
{ Title: "NetEnt", Guid: "59e", xPos: "387" },
{ Title: "MicroGaming", Guid: "19a", xPos: "460" },
{ Title: "Cayetano", Guid: "155", xPos: "533" },
{ Title: "OpenBet", Guid: "cfe", xPos: "607" },
{ Title: "RTG", Guid: "4e6", xPos: "680" },
{ Title: "Cryptologic", Guid: "05d", xPos: "753" },
{ Title: "CTXM", Guid: "51d", xPos: "827" },
{ Title: "Sheriff", Guid: "63e", xPos: "898" },
{ Title: "Vegas Tech", Guid: "a50", xPos: "975" },
{ Title: "Top Game", Guid: "0d0", xPos: "1048" },
{ Title: "Party Gaming", Guid: "46d", xPos: "1121" }
            ];

            for (var s in SoftwareBadges) {
                if (SoftwareBadges[s]["Guid"] == "7e9"){
                   alert(SoftwareBadges[s]["Title"]);
                }
            else{alert('fdsfsdf');}
            }

【问题讨论】:

  • 你在问什么?如何使 else 语句运行一次?或者如何使 else 语句运行的次数与数组中的项目一样多?
  • 首先,不要在数组上使用for-in。其次,我不知道 else 每次都会如何运行,如果这就是你所说的。有一个迭代不会运行。
  • 当发现带有不匹配 Guid 的徽章时,您希望发生什么?您希望它停止检查列表的其余部分,还是希望它继续检查更多匹配的 Guid,但只提醒fdsfsdf 一次?
  • 要检查每一项,别无选择,只能访问列表中的每一项
  • 也许您实际上想要找到具有给定 ID 的对象。如果是这样,请使用var obj = SoftwareBatches.find(o => o.Guid == "7e9")。否则我不知道问题是什么。如果您不希望 else 运行,只需将其删除。这不是必需的。

标签: javascript arrays for-loop if-statement


【解决方案1】:

使用 Array.find()。它返回第一个满足回调条件的元素,并停止遍历其余元素。

SoftwareBadges =
            [
{ Title: "Playtech", Guid: "7e9", xPos: "96" },
{ Title: "BetSoft", Guid: "890", xPos: "169" },
{ Title: "WagerWorks", Guid: "35c", xPos: "242" },
{ Title: "Rival", Guid: "c35", xPos: "314" },
{ Title: "NetEnt", Guid: "59e", xPos: "387" },
{ Title: "MicroGaming", Guid: "19a", xPos: "460" },
{ Title: "Cayetano", Guid: "155", xPos: "533" },
{ Title: "OpenBet", Guid: "cfe", xPos: "607" },
{ Title: "RTG", Guid: "4e6", xPos: "680" },
{ Title: "Cryptologic", Guid: "05d", xPos: "753" },
{ Title: "CTXM", Guid: "51d", xPos: "827" },
{ Title: "Sheriff", Guid: "63e", xPos: "898" },
{ Title: "Vegas Tech", Guid: "a50", xPos: "975" },
{ Title: "Top Game", Guid: "0d0", xPos: "1048" },
{ Title: "Party Gaming", Guid: "46d", xPos: "1121" }
            ];
            
let foundBadge = SoftwareBadges.find(badge => badge.Guid === '7e9');

console.log(foundBadge ? foundBadge : 'asdfads');

【讨论】:

【解决方案2】:

我猜你只想发送一次警报,是这样吗?为此,您需要另一个变量,例如:

SoftwareBadges =
[
{ Title: "Playtech", Guid: "7e9", xPos: "96" },
{ Title: "BetSoft", Guid: "890", xPos: "169" },
{ Title: "WagerWorks", Guid: "35c", xPos: "242" },
{ Title: "Rival", Guid: "c35", xPos: "314" },
{ Title: "NetEnt", Guid: "59e", xPos: "387" },
{ Title: "MicroGaming", Guid: "19a", xPos: "460" },
{ Title: "Cayetano", Guid: "155", xPos: "533" },
{ Title: "OpenBet", Guid: "cfe", xPos: "607" },
{ Title: "RTG", Guid: "4e6", xPos: "680" },
{ Title: "Cryptologic", Guid: "05d", xPos: "753" },
{ Title: "CTXM", Guid: "51d", xPos: "827" },
{ Title: "Sheriff", Guid: "63e", xPos: "898" },
{ Title: "Vegas Tech", Guid: "a50", xPos: "975" },
{ Title: "Top Game", Guid: "0d0", xPos: "1048" },
{ Title: "Party Gaming", Guid: "46d", xPos: "1121" }
];

var enteredInElse = false;

for (var s in SoftwareBadges) {
    if (SoftwareBadges[s]["Guid"] == "7e9"){
        alert(SoftwareBadges[s]["Title"]);
    } else{ enteredInElse = true }
}
if(enteredInElse){
    alert('fdsfsdf');
}

【讨论】:

  • 是的,我只希望 else 语句运行一次,
  • 这样,如果数组有对象,它仍然会同时运行 ForLoop 中的 If 语句和 ELSE 语句,我只希望 if 语句仅在满足条件时运行forloop.if,并且只有在 forloop.if 中的条件不满足时才让 else 语句运行一次
  • 无论我做什么,如果语句为真,它会同时运行 IF 和 The Else 语句
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-28
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-20
相关资源
最近更新 更多