Skip to content
Advertisement

Recursive data in JSON object

JavaScript

}

OK guys I have such JSON structure what I need is to: return all topics in array, in this example it will be:

[“SERVICE_STATUS_PRESETS”, “AIRCRAFT_ACTIVATION”, “OUT_OF_SERVICE”, “PROMO_CODES_REQUESTS”, “BANNERS”, “DOCUMENTS”, “USER”]

I try recursive calls like this, though I only get last three records:

JavaScript

Advertisement

Answer

You could take a recursive approach and check

  • if the handed over data is not an object for checking, then return with empty array,
  • if the object has the wanted property, then return an array with the value of topic,
  • or get the values and make a recursive call with the function and return an array with the result of it.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement