vendredi 20 novembre 2015

if/else statement instead switch

I have this ajax request :

    SJA.ajax(dataToSend, function (respond) {
      var categorySelect = that.$modal.find('.cat-post')[0].selectize;

          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});
          }
        }
    });
  var category: that.$modal.find('.cat-post').val() !== 'null' ? $('.cat-post').val() : null,

And I want to compare received data something like this:

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

But I don't want every time type new values in switch, I want to use if\else statement to receive dynamically new values. I have tried something like this

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

bui it doesn't work. Any ideas?

Aucun commentaire:

Enregistrer un commentaire