js foreach ES6 break
Array.prototype.forEach() – JavaScript | MDN
ES6 In Depth: Iterators and the for-of loop ★ Mozilla Hacks – the Web developer blog
ecmascript 5 – how to stop Javascript forEach? – Stack Overflow
感觉不如直接for循环 带break好用
ES6 for循环
【总结】
然后用如下代码实现了效果:
for (let eachKeyInDict of Object.keys(someDict)){ … } |
代码:
const ROUTER_PAGE = { NONE : { title : "", urlPattern : null, left : { icon : HEADER_ICON.NONE, link : null }, right : { icon : HEADER_ICON.NONE, link : null } }, MAIN : { title : "首页", urlPattern : "/", left : { icon : HEADER_ICON.SWITCH_COW_FARM, link : "/switchCowFarm" }, right : { icon : HEADER_ICON.SCAN, link : "/scan" } }, 。。。 } parsePageType(currentUrl){ let pageType = ROUTER_PAGE.NONE; for (let eachPageKey of Object.keys(ROUTER_PAGE)){ // console.log("——–"); // console.log(eachPageKey); const eachPage = ROUTER_PAGE[eachPageKey]; // console.log(eachPage); // console.log(eachPage.title); // console.log(eachPage.urlPattern); const toCompareUrl = `${ENV_PREFIX}${eachPage.urlPattern}`; // console.log(`toCompareUrl=${toCompareUrl}`); if (currentUrl === toCompareUrl){ pageType = eachPage; break; } } … } |
转载请注明:在路上 » 【已解决】ES6中如何实现foreach中带break