Javascript如何才能使'else'输出只有一次?

时间:2017-07-25 02:35:05

标签: javascript arrays for-loop multidimensional-array

我一直在尝试使用二维数组的for循环,显然我是javascript和编程的新手一般..我试图找到数组内的var如果找到然后输出成功消息,如果没有输出失败的消息。每当我输出代码输出失败的消息时,它会在控制台上显示4次,成功消息仍会显示..

如果失败则预期输出:

No matching keyword was found

结果输出:

The keyword String is found 
No matching keyword was found 
No matching keyword was found 
No matching keyword was found 
No matching keyword was found
newArray = [["String","Sa"],[1,2,35]];


var find = "String";
for(var i=0; i< newArray.length;i++){
    for(var j=0; j < newArray.length + i; j++){
        if (newArray[i][j] === find){
            console.log("The keyword " + find + " is found");
        } else {
            console.log("No matching keyword was found");
        }
    }

}

4 个答案:

答案 0 :(得分:0)

就像他们所说的那样,使用变量作为指标。

newArray = [["String","Sa"],[1,2,35]];


var find = "String";
var found = false;
for(var i=0; i< newArray.length;i++){
    for(var j=0; j < newArray[i].length + i; j++){
        if (newArray[i][j] === find){
            console.log("The keyword " + find + " is found");
            found = true;
        }
    }

}
if (!found) {
    console.log("No matching keyword was found");
}

请分析并比较我们的代码,以防止您将来提出类似的问题。

现在,在我们&#34;发现&#34;之后,尝试优化上述内容,而不是迭代其他元素。它的匹配。

答案 1 :(得分:0)

你的第二个循环不正确。

在你的内循环中,你必须循环newArray[i] 此外,如果搜索成功,请使用break

还使用变量来表示操作已结束。

 newArray = [["String","Sa"],[1,2,35]];

 var find = "String";

  var end = false;
   for(var i=0; i< newArray.length;i++){
        for(var j=0; j < newArray[i].length; j++){
               if (newArray[i][j] === find){
                    console.log("The keyword " + find + " is found");
                     end = true;
                     break;
                } else {
                     console.log("No matching keyword was found");
                 }
          }
           if (end) {
                 break;
            }
     }

答案 2 :(得分:0)

&#13;
&#13;
newArray = [["String","Sa"],[1,2,35]];


var find = "String";
for(var i=0; i< newArray.length;i++){
    for(var j=0; j < newArray[i].length + i; j++){// <--- you want to iterate only as many times as the inner array elements. Right now you iterate over the same array index as the first loop.
        if (newArray[i][j] === find){
            alert("The keyword " + find + " is found");
            return; //<-- end script after result is found
        } 
    }
}
//Will only get here if the return was not hit
alert("No matching keyword was found");
&#13;
&#13;
&#13;

你看到它4次,因为循环将继续运行,直到它迭代了所有东西。实现所需目标的一种方法是在找到结果后结束脚本。

如果你想采用光滑的es6方法:

&#13;
&#13;
newArray = [["String","Sa"],[1,2,35,"String"]];
const term = "String2";
const isFound = newArray.some(e=>e.find(e=>e===term));
alert(`The keyword has ${isFound ? "" : "not"} been found`);
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

使用布尔值来存储一个标记&#34; isFound&#34;,并在找到时中断。

newArray = [["String","Sa"],[1,2,35]];
var find = "String";
var isFound = false;
for(var i=0; i< newArray.length;i++){
    for(var j=0; j < newArray.length + i; j++){
        if (newArray[i][j] === find){
            console.log("The keyword " + find + " is found");
            isFound = true;
            break;
        }
    }
}
if (isFound == false){
console.log("No matching keyword was found");
}