我有一个相当重复的switch case语句,在我学习最简单的做事方式的过程中,我想转向SO,看看是否有更优雅的解决方案:
switch(id)
{
case 'ib-02a':
if(direction == 'left')
setHash('ib-02b');
break;
case 'ib-02b':
if(direction == 'right')
setHash('ib-02a');
if(direction == 'left')
setHash('ib-02c');
break;
case 'ib-02c':
if(direction == 'right')
setHash('ib-02b');
if(direction == 'left')
setHash('ib-02d');
break;
case 'ib-02d':
if(direction == 'right')
setHash('ib-02c');
break;
case 'ib-03a':
if(direction == 'left')
setHash('ib-03b');
break;
case 'ib-03b':
if(direction == 'right')
setHash('ib-03a');
if(direction == 'left')
setHash('ib-03c');
break;
case 'ib-03c':
if(direction == 'right')
setHash('ib-03b');
if(direction == 'left')
setHash('ib-03d');
break;
case 'ib-03d':
if(direction == 'right')
setHash('ib-03c');
break;
case 'pb-05a':
if(direction == 'left')
setHash('pb-05b');
break;
case 'pb-05b':
if(direction == 'right')
setHash('pb-05a');
if(direction == 'left')
setHash('pb-05c');
break;
case 'pb-05c':
if(direction == 'right')
setHash('pb-05b');
if(direction == 'left')
setHash('pb-05d');
break;
case 'pb-05d':
if(direction == 'right')
setHash('pb-05c');
break;
}
我正在阅读滑动事件,如果我正在滑动的元素的ID与ib-02 *,ib-03 *或pb-05 *匹配,我正在为相应的ID调用setHash函数。如果我在* a上滑动,我向左滑动到* b。如果我在* b上滑动,我向右滑动到* a并向左滑动到* c。所以依此类推,总是在* a和* d之间。
必须采用较少重复的方式来做到这一点,但我不确定最佳方法是什么。
答案 0 :(得分:2)
如何将它们映射到对象?然后只需使用带有检索值的setHash
。
var ids = {
'pb-05c' : {
left : 'pb-05d',
right : 'pb-05b'
}
...
}
function setHashes(id,direction){
if(id && ids[id]){
id = ids[id];
if(direction && id[direction]){
setHash(id[direction]);
}
}
}
这是所有检索和无条件评估,这可能有利于提高性能。
答案 1 :(得分:2)
有4个主要案例a
,b
,c
和d
,您可以将switch语句作为这些字符串的基础,请尝试以下操作:
var c = id.slice(0, 5); // "ib-02" or "ib-03" or "ib-04" ...
var which = id.slice(-1); // "a" or "b" or "c" or "d"
switch(which) {
case 'a':
if(direction == 'left')
setHash(c+'b');
break;
case 'b':
if(direction == 'right')
setHash(c+'a');
if(direction == 'left')
setHash(c+'c');
break;
case 'c':
if(direction == 'right')
setHash(c+'b');
if(direction == 'left')
setHash(c+'d');
break;
case 'd':
if(direction == 'right')
setHash(c+'c');
break;
}
答案 2 :(得分:1)
你可以像这样驱动整个数据表:
var logicData = {
// format is the id first and then an array with the left, then right value for the hash
// leave an item as an empty string if you don't ever want to go that direction
'ib-02a': ['ib-02b', ''],
'ib-02b': ['ib-02c', 'ib-02a'],
'ib-02c': ['ib-02d', 'ib-02d']
// fill in the rest of the data table here
};
function setNewHash(id, direction) {
var hash, data = logicData[id];
if (data) {
if (direction == 'left') {
hash = data[0];
} else if (direction == 'right') {
hash = data[1];
}
if (hash) {
setHash(hash);
}
}
}
答案 3 :(得分:1)
id='ib-02a'; //you have string id, this one is for demo
id=[id.slice(0,--id.length), id.charAt(--id.length)];
switch(id[1]){
case 'a':
if(direction == 'left'){setHash(id[0]+'b');}
break;
case 'b':
if(direction =='right'){setHash(id[0]+'a');}
if(direction == 'left'){setHash(id[0]+'c');}
break;
case 'c':
if(direction == 'right'){setHash(id[0]+'b');}
if(direction == 'left'){setHash(id[0]+'d');}
break;
case 'd':
if(direction == 'right'){setHash(id[0]+'c');}
break;
}
如果案例b和c仅为“左”或“右”,则可以在if语句中使用else
。
答案 4 :(得分:1)
我喜欢undefined和GitaarLab的一般方向,他们实际上解决了算法并且刚刚实现了算法。要进行审核,算法基本上是left
递增最终字母,right
递减ID的最后一个字母,但不要低于a
或高于d
。所以,我做了一个紧凑的实现,我将最后一个字母转换为数字并直接递增或递减,而不是使用if / else或case语句:
function setNewHash(id, direction) {
var base = id.substr(0, 5);
var tag = id.charCodeAt(5), newTag;
var nav = {left: 1, right: -1};
var delta = nav[direction];
if (delta) {
tag += delta;
newTag = String.fromCharCode(tag);
if (newTag >= 'a' && newTag <= 'd') {
setHash(base + newTag);
}
}
}
答案 5 :(得分:0)
您可以将ID分成不同的部分,然后在setHash
之前重建它们。
function chunkId(id) {
// use a regex or string split or something to convert
// "ib-05a" to ["ib-05", "a"]
return ["ib-05", "a"];
}
function next(str) {
// return the next letter in the alphabet here
return "b";
}
function prev(str) {
// return the prev letter in the alphabet here
return "b";
}
function swipe (id, direction) {
var array = chunkId(id);
// you can only swipe left if not on "d"
if (direction === "left" && id[1] != "d") {
setHash(id[0] + next(id[1])); // build hash based on next character
}
// you can only swipe right if not on "a"
if (direction === "right" && id[1] != "a") {
setHash(id[0] + prev(id[1])); // build hash based on prev character
}
}
答案 6 :(得分:0)
您可以切换ID和方向检查以使其更清晰:
switch (direction) {
case 'left':
switch (id) {
case 'ib-02a': setHash('ib-02b'); break;
case 'ib-02b': setHash('ib-02c'); break;
case 'ib-02c': setHash('ib-02d'); break;
case 'ib-03a': setHash('ib-03b'); break;
case 'ib-03b': setHash('ib-03c'); break;
case 'ib-03c': setHash('ib-03d'); break;
case 'pb-05a': setHash('pb-05b'); break;
case 'pb-05b': setHash('pb-05c'); break;
case 'pb-05c': setHash('pb-05d'); break;
}
break;
case 'right':
switch (id) {
case 'ib-02b': setHash('ib-02a'); break;
case 'ib-02c': setHash('ib-02b'); break;
case 'ib-02d': setHash('ib-02c'); break;
case 'ib-03b': setHash('ib-03a'); break;
case 'ib-03c': setHash('ib-03b'); break;
case 'ib-03d': setHash('ib-03c'); break;
case 'pb-05b': setHash('pb-05a'); break;
case 'pb-05c': setHash('pb-05b'); break;
case 'pb-05d': setHash('pb-05c'); break;
}
break;
}
然后你可以通过分割id:
来简化var first = id.substr(0, 5);
var last = id.substr(6);
switch (direction) {
case 'left':
switch (last) {
case 'a': setHash(first + 'b'); break;
case 'b': setHash(first + 'c'); break;
case 'c': setHash(first + 'd'); break;
}
break;
case 'right':
switch (last) {
case 'b': setHash(first + 'a'); break;
case 'c': setHash(first + 'b'); break;
case 'd': setHash(first + 'c'); break;
}
break;
}