0
 var args = {VehicleManipulation:[{
                        'a:Amount':[Amount],
                        'a:Comment':[Comment],
                        'a:Date':[date],
                        'a:DispatchingVehicleManipulationReasonID':[ReasonID],
                        'a:Distance':[Distance],
                     frEndTime? 'a:FirstDeadheadTripEndTime':[frEndTime], :'', 
                        'a:FirstDeadheadTripStartTime':[frStartTime],
                        'a:SecondDeadheadTripEndTime':[secondEndTime],
                        'a:SecondDeadheadTripStartTime':[secondStartTime],
                        'a:SubsystemID':[SubsystemNO],
                        'a:VehicleID':[VehicleID],
                        'a:VehicleManipulationID':[VehicleManipulationID],
                        'a:VehicleName':[VehicleName],
        }]};

How to put ternary operator to my example?

if variable has value put this line of code 'a:FirstDeadheadTripEndTime':[frEndTime], if not do not put anything

now I have syntax error

Bobek
  • 757
  • 3
  • 7
  • 15

2 Answers2

1

You need the property first. And then the conditional operator.

'a:FirstDeadheadTripEndTime': frEndTime ? [frEndTime] : '', 

if frEndTime dont have value I do not want to put 'a:FirstDeadheadTripEndTime'

Then you could use another object with a spreading object (if possible)

'a:Distance':[Distance],
...(frEndTime ? { 'a:FirstDeadheadTripEndTime': [frEndTime] } : {}),
'a:FirstDeadheadTripStartTime':[frStartTime],
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You are missing key in your object which is incorrect. You need to do:

  var args = {
  VehicleManipulation: [{
    'a:Amount': [Amount],
    'a:Comment': [Comment],
    'a:Date': [date],
    'a:DispatchingVehicleManipulationReasonID': [ReasonID],
    'a:Distance': [Distance],
    'a:FirstDeadheadTripStartTime': [frStartTime],
    'a:SecondDeadheadTripEndTime': [secondEndTime],
    'a:SecondDeadheadTripStartTime': [secondStartTime],
    'a:SubsystemID': [SubsystemNO],
    'a:VehicleID': [VehicleID],
    'a:VehicleManipulationID': [VehicleManipulationID],
    'a:VehicleName': [VehicleName],
  }]
};
if(frEndTime){
  args.VehicleManipulation[0]['a:FirstDeadheadTripEndTime'] = frEndTime
}

Doing this will only add the key a:FirstDeadheadTripEndTime if frEndTime value exist.

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62