Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const theObject = {
nested1: {
key1: "value1",
nested2: {
key2: "value2"
}
},
key3: "value3",
key4: ["test", "test1"]
};
function flatKeys(obj, prefix = "") {
return Object.keys(obj).reduce(function(res, el) {
if (Array.isArray(obj[el])) {
return res;
} else if (typeof obj[el] === "object" && obj[el] !== null) {
return [...res, ...flatKeys(obj[el], prefix + el + ".")];
} else {
return [...res, prefix + el];
}
}, []);
}
const objectKeys = flatKeys(theObject).reduce((a, b) => {
a[b] = null;
return a;
}, {});
document.write(`<pre>${JSON.stringify(objectKeys, null, 2)}</pre>`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.