I wonder if it would be possible for me to employ a recursive function to read all the attributes and properties of an object including the nested properties and such. for example, if I have an object:
var mObj = {};
mObj.countries = [];
mObj.country = {};
mObj.country.states = [];
mObj.country.state = {};
mObj.country.state = {};
I am sure you get the picture. If it was just a simple object then I can employ "for in" loop, and perhaps nested "for in" loop, an object has numerous nested levels then using nested "for in" loops becomes somewhat chaos. I thought it would wonderful to employ recursion. Any help insight to this would highly appreciated.
Thank you.
console.log()an object, it will print all of its properties recursively. I suggest you use one of those to develop, unless you want to use this information for something other than debugging.mObj.country.state = {};would in practice refer to two different states right? It looks like you can use getOwnPropertyNames to define a base case according to CMS. getOwnPropertyNames can be recursively called on properties which themselves have properties, I would imagine, and it will return an object whose length is 0 if there are no properties in the object on which it is called.