Is there a way you could loop through an object with multiple nested levels of objects and arrays and extract only all the bottom level property values?
In this example I would want it to console.log: "Live JSON generator", 3.1, "2014-06-25T00:00:00.000Z", true and further down: "Jane Doe", "888-555-1212", "spouse"
let test = { "product": "Live JSON generator", "version": 3.1, "releaseDate": "2014-06-25T00:00:00.000Z", "demo": true, "person": { "id": 12345, "name": "John Doe", "phones": { "home": "800-123-4567", "mobile": "877-123-1234" }, "email": [ "jd@example.com", "jd@example.org" ], "dateOfBirth": "1980-01-02T00:00:00.000Z", "registered": true, "emergencyContacts": [ { "name": "Jane Doe", "phone": "888-555-1212", "relationship": "spouse" }, { "name": "Justin Doe", "phone": "877-123-1212", "relationship": "parent" } ] } } for(var key in test){ console.log(test[key]); } console.log(test);
The JSON.parse
reviver and JSON.stringify
replacer can be an easy way to check all values :
var o = {"product":"Live JSON generator","version":3.1,"releaseDate":"2014-06-25T00:00:00.000Z","demo":true,"person":{"id":12345,"name":"John Doe","phones":{"home":"800-123-4567","mobile":"877-123-1234"},"email":["jd@example.com","jd@example.org"],"dateOfBirth":"1980-01-02T00:00:00.000Z","registered":true,"emergencyContacts":[{"name":"Jane Doe","phone":"888-555-1212","relationship":"spouse"},{"name":"Justin Doe","phone":"877-123-1212","relationship":"parent"}]}} JSON.stringify(o, (k, v) => typeof v == 'object' ? v : console.log(k, ':', v))
Its not working because you are trying to read a JSON notated object wich is something different as an javascript object.
Try reading this article wich explains its properly.The tricky part is in the end.
And that, my son is where the answer lies!
Good luck and happy coding!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.