简体   繁体   English

if / else语句改为切换

[英]if/else statement instead switch

I have this ajax request : 我有这个ajax请求:

    SJA.ajax(dataToSend, //this is internal method, which sends requests to the urls  
function (respond) {

      var categorySelect = that.$modal.find('.cat-post')[0].selectize; //using selectize.js plugin for selects. 

          if (respond) {
          callback && callback(respond);
          for (var i in respond) {
            //console.log(respond[i].id);
            //console.log(respond[i].name);
            categorySelect.addOption({value: respond[i].id, text: respond[i].name}); //adding new options to a select field.
          }
        }
    });
  var category: that.$modal.find('.cat-post').val() //this is option value in select field !== 'null' ? $('.cat-post').val() : null, //this variable is responsible for keeping selected data. 

And then I compare selected data (because I need to get some string value to use it in table): 然后我比较选定的数据(因为我需要获取一些字符串值才能在表中使用它):

var categoryName = 'all';
        switch (category) {
          case '0' // compare with respond id: 
            categoryName = "all" // compare with respond name ;
            break;
          case '1':
            categoryName = "Health";
            break;
          case '2':
            categoryName = "Cars";
            break;
        }

And after that I am adding new td in my table. 之后,我要在表中添加新的td。

  $tbody.append(['<tr data-id="' + row.id + '">',
'<td class="restricted-view-hide view-mode2-hidden view-mode21-hidden">'  + categoryName + '</td>', '</tr>'].join(''));

But I don't want every time type new values in switch, I want to use something to receive dynamically new values. 但我不想每次都在switch中键入新值,而是想使用某种方式来动态接收新值。 I have tried something like this: 我已经尝试过这样的事情:

categoryName = respond[i].id != null ? respond[i].name : "any";

bui it doesn't work. 但是它不起作用。 Any ideas? 有任何想法吗?

Right. 对。 Assumptions: 假设:

1) you are aware that the conditional statement you have posted uses respond.id and respond.name, where your switch statement uses $(".cat-post").val(), and that these two methods are likely to return completely different values. 1)您知道您发布的条件语句使用response.id和response.name,而您的switch语句使用$(“。cat-post”)。val(),并且这两种方法很可能完全返回不同的值。

2) The conditional statement you have posted uses the variables you want it to use, and it's just not giving the expected answer - that is, in the situation where respond.id isn't valid it's not giving you "any". 2)您发布的条件语句使用您希望它使用的变量,而只是没有给出预期的答案-也就是说,在response.id无效的情况下,它不会给您“任何”。

If this is the case, then the solution is simple enough. 如果是这种情况,那么解决方案就足够简单了。 An element with no id returns "" for .id, not null, so check whether respond.id != "", not != null. 没有id的元素为.id返回“”,不为null,因此请检查是否response.id!=“”,而不是!= null。

categoryName = respond[i].id != "" ? respond[i].name : "any";

That said... 那个...

categoryName = respond[i].id != null ? respond[i].name : "any";

This just doesn't look right. 这看起来不正确。 It basically says "If my respond element has an id, then use its name value as categoryName". 它基本上说:“如果我的response元素具有ID,则将其名称值用作categoryName”。 Is this what you want? 这是你想要的吗? Can you be sure that respond[i].name will always be valid if respond[i].id is? 如果response [i] .id是,您可以确定response [i] .name始终有效吗? Why not just check whether respond[i].name is != ""? 为什么不只检查response [i] .name是否为!=“”? Is there some doubt over the validity of the name attribute if the id attribute isn't set? 如果未设置id属性,是否会对name属性的有效性存在疑问? Using 运用

categoryName = respond[i].name != "" ? respond[i].name : "any";

would appear on face value to make far more sense. 会出现在面值上,以使其更有意义。

It's also worth noting that assumption 1) is a pretty big assumption. 值得注意的是,假设1)是一个很大的假设。 If your code is currently working the way you want it to, but you want to make it more flexible, unless there's some serious duplication of data going on (where $(.cat-post)[0].val() is set to some value determined by respond[i].name (or vice-versa)), implementing what you have above is going to break your code. 如果您的代码当前正在按照您希望的方式工作,但您想使其变得更加灵活,除非正在进行严重的数据重复(其中$(。cat-post)[0] .val()设置为一些由response [i] .name(反之亦然)确定的值,实现上面的内容将破坏代码。

Note that this only really applies if your code is fundamentally working, but giving you the wrong values. 请注意,这仅在代码从根本上起作用的情况下才适用,但是给您错误的值。 if it's broken, not giving you any response at all, then you need to be more specific about what you mean by "it doesn't work". 如果它损坏了,根本没有给您任何答复,那么您需要更加具体地说明“它不起作用”的含义。

Dunno if it could change anything but try : Dunno是否可以改变但尝试:

categoryName = (respond[i].id != null) ? respond[i].name : "any";

instead maybe ^^ 相反,也许^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM