Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Updating JSON structure based on id

I was trying to update the town name in the below-given JSON structure.


    "City":[
   {
      "Name":"Delhi",
      "id":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd",
      "Towns":[
         {
            "Name":"MG Colony",
            "conditionId":"60d1f5eb-0222-4a84-879b-6699b0cfc1a4",
            "cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd"
         },
         {
            "Name":"DLF Colony",
            "conditionId":"60d1f5eb-0222-4a84-879b-7749b0cfc1a4",
            "cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd"
         }
      ]
   },
   {
      "Name":"Pune",
      "id":"c5d58bef-f1c2-4b7c-a6d7-f64df12321ax",
      "Towns":[
         {
            "Name":"Hadapsar",
            "conditionId":"60d1f5eb-0222-4a84-879b-6699b0cfc1x4",
            "cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321ax"
         },
         {
            "Name":"Magarpatta",
            "conditionId":"60d1f5eb-0222-4a84-879b-7749b0cfc1f4",
            "cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bax"
         }
      ]
   }
]

I wanted to change the town name from "Hapdasar" to "Viman Nagar" if my cid matches that of Hadapsar Town

Output which I wanted was:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


   "City":[
   {
      "Name":"Delhi",
      "id":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd",
      "Towns":[
         {
            "Name":"MG Colony",
            "conditionId":"60d1f5eb-0222-4a84-879b-6699b0cfc1a4",
            "cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd"
         },
         {
            "Name":"DLF Colony",
            "conditionId":"60d1f5eb-0222-4a84-879b-7749b0cfc1a4",
            "cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bd"
         }
      ]
   },
   {
      "Name":"Pune",
      "id":"c5d58bef-f1c2-4b7c-a6d7-f64df12321ax",
      "Towns":[
         {
            "Name":"Viman Nagar",
            "conditionId":"60d1f5eb-0222-4a84-879b-6699b0cfc1x4",
            "cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321ax"
         },
         {
            "Name":"Magarpatta",
            "conditionId":"60d1f5eb-0222-4a84-879b-7749b0cfc1f4",
            "cid":"c5d58bef-f1c2-4b7c-a6d7-f64df12321bax"
         }
      ]
   }
]

I was using js map to iterate but was confused about how to replicate the exact structure.

>Solution :

Well, map alone is not enough to solve your problem, since you have two nested arrays. Maybe you can consider the possibility to use maptwice?
For example:

var cid = "c5d58bef-f1c2-4b7c-a6d7-f64df12321ax";
var newName = "Viman Nagar";
City = City.map(function(city) {
  city.towns = city.towns.map(function(town) {
    if (town.cid === cid)
      town.name = newName;
    return town;
  });
  return city;
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading