If I had an object:
var dog= {
name: "Max",
age: 5,
sex: undefined,
details: {
color: "black",
breed: undefined
}
}
And I wanted to get the paths of the properties with undefined values. How could I iterate through all of the properties, including the nested ones?
Currently, I have this vanilla js method for an object without nested properties:
function getUndefinedPaths(o, name) {
var paths = [];
for (var prop in o) {
if (o[prop] === undefined) {
paths += name + "." + prop + "\n";
}
}
return paths;
}
// getUndefinedPaths(dog, "dog") only returns "dog.sex" and not "dog.details.breed" which is also undefined.
I'm stuck. Can someone help out on how to get paths of those undefined values in a nested property of a js object? I'm attempting this in vanilla javascript only. Thanks in advance.