在for的遍历循环中,else之后的结果alert如何让其只弹出一次?

在一个for循环中当不满足之前的两个if条件时,则弹出alert提示框,由于做了遍历查询,所以alert会根据数组数量多次弹出,请教这个应该如何做截断,让其判断后只弹出一次?

https://img.mukewang.com/5c0a316c0001edb405390755.jpg

目前这样写会根据json数据的条数,弹窗就会弹出三次。。。

蝴蝶刀刀
浏览 620回答 1
1回答

德玛西亚99

function f(jsons, num) {&nbsp; &nbsp; var num8 = num.substr(0, 8);&nbsp; &nbsp; var num6 = num.substr(0, 6);&nbsp; &nbsp; var result = '';&nbsp; &nbsp; var inArr = 0;&nbsp; &nbsp; for (var i = 0, len = jsons.length; i < len; i++) {&nbsp; &nbsp; &nbsp; &nbsp; var id = jsons[i].id;&nbsp; &nbsp; &nbsp; &nbsp; if (id == num8) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result8 = jsons[i].info.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#card_info").fadeIn("500");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#close").fadeIn("500");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inArr++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (id == num6) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result6 = jsons[i].info.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#card_info").fadeIn("500");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#close").fadeIn("500");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inArr++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; !inArr && alert("不符合条件");&nbsp; &nbsp; return result8 ? result8 : result6;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript