How to expand single level nested hashes on the fly?
问题 I have an object where a few of the keys are nested single level hashes. In this example only b is nested. const j = { a: 'A', b: { bb: 'BB', bbb: 'BBB', }, c: 'C' }; Question What I am looking for is a way to loop over the object and if a key is a nested object, then print its keys instead. a bb bbb c Does anyone know how to do that? 回答1: You can do this recursively: function printKeys(obj) { for (const [key, val] of Object.entries(obj)) { if (typeof val === "object") { printKeys(val); }