javascript - hashmap - javascript switch(true)执行false语句?

64 5

下面是一个小例子:


var idm = {0:1, 3:1, 9:1, 10:1, 11:1, 12:1, 20:1, 21:1, 23:1}



var findNeighbors = function(b) {



 var u,d,l,r,lRow,rRow;


 var currentBuilding = parseInt(b);


 var currRow = Math.floor(currentBuilding/column);



 //remove value from map so we dont recount it.


 delete idm[currentBuilding];



 u = currentBuilding - column;


 d = currentBuilding + column;


 l = currentBuilding - 1;


 lRow = Math.floor(l/column);


 r = currentBuilding + 1;


 rRow = Math.floor(r/column);



 console.log("current idx:" + currentBuilding);


 console.log("u:" + u +", d:" + d +", l:" + l +" r:" + r);


 // debugger;


 switch(true) {


 case (idm.hasOwnProperty(u) === true):


 console.log((idm.hasOwnProperty(u)));


 console.log("map has" + currentBuilding +" -> u:" + u);


 findNeighbors(u);


 case (idm.hasOwnProperty(d) === true):


 console.log((idm.hasOwnProperty(d)));


 console.log("map has" + currentBuilding +" -> d:" + d);


 findNeighbors(d);


 case (lRow === currRow && idm.hasOwnProperty(l) === true):


 console.log((lRow === currRow && idm.hasOwnProperty(l)));


 console.log("map has" + currentBuilding +" -> l:" + l);


 findNeighbors(l);


 case (rRow === currRow && idm.hasOwnProperty(r) === true):


 console.log((rRow === currRow && idm.hasOwnProperty(r)))


 console.log("map has" + currentBuilding +" -> r:" + u);


 findNeighbors(r);


 }


 console.log("---------------------------");


 }



时间: 原作者:

60 5

你要编写有效的代码而不是看起来很酷的代码。你忘记了break语句,因此第一个匹配之后没有评估其他case表达式,

请使用标准的if /else。

原作者:
...