user2847749
user2847749

Reputation: 369

syntax of multiple ternary operators in ajax call in javascript

I'm trying to have some if statements in a javascript ajax call and I feel like it should be possible, just maybe my syntax is wrong. I'm trying to create the schedule part below:

$.ajax({
                type    :  'POST',
                name    :  'Submitting Request',
                url     :   '/breadcrumbs/crumb',
                dataType: 'json',
                data    : {
                    parameters  : paramsObj,
                    schedule: {       paramsObj.isfirst   ? firstSched
                                    : paramsObj.issecond  ? secondSched
                                    : paramsObj.isthird   ? thirdSched
                              }
                          },
            success  : function(){},
            error   : function(jqXHR, status, error) {}
       });

I keep getting "Uncaught SyntaxError: Unexpected token ." around this block, specifically on the schedule line:

             data   : {
                parameters  : paramsObj,
                schedule: {       paramsObj.isfirst    ? firstSched
                                : paramsObj.issecond ? secondSched
                                : paramsObj.isthird ? thirdSched
                          }
                      },

for schedule in particular, this is what i'm trying to do with the ternary operators:

if( paramsObj.isfirst === true) { schedule = firstSched}
if( paramsObj.issecond === true){ schedule = secondSched}
if( paramsObj.isthird === true) { schedule = thirdSched}

Does anyone know what I'm doing wrong?

Upvotes: 1

Views: 2831

Answers (2)

Dave
Dave

Reputation: 10924

Nested ternaries can be difficult to read. I'd suggest rewriting this as an Immediately-executed function expression (IIFE) to make it much more readable:

schedule: (function() {
    if (paramsObj.isfirst)
        return firstSched;
    else if (paramsObj.issecond)
        return secondSched;
    else if (paramsObj.isthird)
        return thirdSched;
    else
        return undefined;
})();

Upvotes: 2

Seth Flowers
Seth Flowers

Reputation: 9190

Just the relevant field:

schedule: paramsObj.isfirst ? firstSched : 
    paramsObj.issecond ? secondSched :
    paramsObj.isthird ? thirdSched : undefined

Full call:

$.ajax({
    type: 'POST',
    name: 'Submitting Request',
    url: '/breadcrumbs/crumb',
    dataType: 'json',
    data: {
        parameters: paramsObj,
        schedule: paramsObj.isfirst ? firstSched : 
            paramsObj.issecond ? secondSched :
            paramsObj.isthird ? thirdSched : undefined,
        success: function(){},
        error: function(jqXHR, status, error) {}
   });

Note

As others have pointed out, chaining ternaries can be hard to read, which can lead to bugs and maintenance problems. I personally don't mind chaining them, as long as they are broken out per line, as shown, so that they are easy to follow...

Upvotes: 6

Related Questions