0

So im trying to create a relatively simple dictionary see snippet below:

var res = {"id":$( "#chose_partner option:selected" ).val(),
                   "name":$( "#chose_partner option:selected" ).text()};

        var_current_partner_id = $("#chose_partner option:selected").val();

        try{
            //if dictionary already has the "trip_stop" key then append value to its value as its a list
            var_user_record.trip_info['trip_stops'].push('res');
            console.log('found stops so adding %o ', var_user_record);
        }catch (e){
            //if "trips_stop" doesn't exist then declare it as a list and append my value               
            console.log('error looking for specific trip %o %o %o ',var_user_record['trip_info'],var_user_record.trip_info,var_user_record);

            var_user_record['trip_info']['trip_stops'] = [];
            var_user_record['trip_info']['trip_stops'].push(res);

        }

The logic if dictionary already has the "trip_stop" key then append value to its value as its a list.

if "trips_stop" doesn't exist then declare it as a list and append my value.

The output is so unexpected see below

enter image description here

They are two things happening here

  1. THe logging is logging a value that is declared in the next line ? How is that possible unless console.log can be delyed on chrome?
  2. The third object is the main dictionary but doesn't show the recently added keys even though refrencing it through var_user_record['trip_info'],var_user_record.trip_info yields values but not var_user_record doesn't show it
sqwale
  • 554
  • 3
  • 24
  • You probably shouldn't use `try/catch` for program flow. You can easily check if `var_user_record.trip_info['trip_stops']` exists. – Frank Modica Aug 09 '18 at 20:43
  • FYI, `$( "#chose_partner option:selected" ).val()` can be just `$("#chose_partner").val()`. – Barmar Aug 09 '18 at 20:53

0 Answers0