Skip to content
Advertisement

group by keys and its value, where a key has many keys with values

In json format:

form_data = {"block_1":{"mobile_number":"8745965123"},"block_2":{"diabetic":"Yes","diabetic_insulin":"INSULIN","major_illnesses_in_the_past":"qwe","major_surgeries_in_the_past":"qwerf"},"block_4":{"orientation_time":"TIME"},"block_5":{"skin_condition_intact":"INTACT"},"block_6":{"transferring_self":"SELF"},"block_7":{"mobility_independent":"INDEPENDENT","assistive_device_walker":"WALKER","upper_limbs_normal":"NORMAL","lower_limbs_normal":"NORMAL"},"block_8":{"oral_status_dry_mouth":"DRY MOUTH","diet_regular":"REGULAR"},"block_9":{"bowels_self_care":"SELF CARE"},"block_10":{"what_decrease_helps_to_reduce_pain_medication":"MEDICATION","what_decrease_helps_to_reduce_pain_any_other_method":"ANY OTHER METHOD"},"block_11":{"have_you_had_a_fail_in_last_12months":"Yes"},"block_12":{"medication_regimen_medication_1":"qwerfg","medication_regimen_dosage_1":"11"},"block_13":{"physical_problems_difficulty_in_hearing":"DIFFICULTY IN HEARING"},"block_14":{"eating_drinking_self":"SELF"},"others":{"daily_routinr_optional":"on","assesors_name":"qwert"},"block_15":{"6am_mon":"qwer","7am_tue":"wer","8am_wed":"wert","9am_fri":"wert"},"block_16":{"week_1":"qwer","week_2":"wer"},"block_17":{"environmental_assessment_family_members":"qwer"},"block_18":{"facilities_for_caregivers_accomodation":"qwert"},"block_19":{"sevice_required_live_in_care":"LIVE-IN-CARE"},"block_21":{"ventilator_evo":"Ventilator-EVO"}}

then to object:

var data_key = JSON.parse(form_data);

I want output like:

block_1
    mobile_number - 8745965123
block_2
    diabetic - Yes
    diabetic_insulin - INSULIN
    major_illnesses_in_the_past - qwe
    major_surgeries_in_the_past - qwerf

...continue...

Thanks in advance!

Advertisement

Answer

var form_data = {"block_1":{"mobile_number":"8745965123"},"block_2":{"diabetic":"Yes","diabetic_insulin":"INSULIN","major_illnesses_in_the_past":"qwe","major_surgeries_in_the_past":"qwerf"},"block_4":{"orientation_time":"TIME"},"block_5":{"skin_condition_intact":"INTACT"},"block_6":{"transferring_self":"SELF"},"block_7":{"mobility_independent":"INDEPENDENT","assistive_device_walker":"WALKER","upper_limbs_normal":"NORMAL","lower_limbs_normal":"NORMAL"},"block_8":{"oral_status_dry_mouth":"DRY MOUTH","diet_regular":"REGULAR"},"block_9":{"bowels_self_care":"SELF CARE"},"block_10":{"what_decrease_helps_to_reduce_pain_medication":"MEDICATION","what_decrease_helps_to_reduce_pain_any_other_method":"ANY OTHER METHOD"},"block_11":{"have_you_had_a_fail_in_last_12months":"Yes"},"block_12":{"medication_regimen_medication_1":"qwerfg","medication_regimen_dosage_1":"11"},"block_13":{"physical_problems_difficulty_in_hearing":"DIFFICULTY IN HEARING"},"block_14":{"eating_drinking_self":"SELF"},"others":{"daily_routinr_optional":"on","assesors_name":"qwert"},"block_15":{"6am_mon":"qwer","7am_tue":"wer","8am_wed":"wert","9am_fri":"wert"},"block_16":{"week_1":"qwer","week_2":"wer"},"block_17":{"environmental_assessment_family_members":"qwer"},"block_18":{"facilities_for_caregivers_accomodation":"qwert"},"block_19":{"sevice_required_live_in_care":"LIVE-IN-CARE"},"block_21":{"ventilator_evo":"Ventilator-EVO"}};

Object.keys(form_data).forEach((x)=>{
    console.log(x);
  Object.keys(form_data[x]).forEach((y)=>{
      console.log("t"+y+" - "+form_data[x][y]);
    })
})
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement