Skip to content

Instantly share code, notes, and snippets.

@kjr247
Created October 14, 2017 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjr247/b062167602a52f65b5c3fd9fbf414b66 to your computer and use it in GitHub Desktop.
Save kjr247/b062167602a52f65b5c3fd9fbf414b66 to your computer and use it in GitHub Desktop.
This is a first draft crack at traversing a tree looking for undefined. Could definitely be more performant, but it works for what I need.
function filterDeep(humans) {
return humans.map((val, key) => {
if(Array.isArray(val)) {
filterDeep(val);
} else if(typeof val === "object") {
filterDeep(Object.values(val));
} else if (val === undefined) {
console.log(typeof val, val, key);
return true;
}
}).length > 0;
};
const people = [
{
name: undefined,
sex: 'male',
children: [
{
name: 'Jeffrey',
sex: 'male',
children: [
{
name: 'Alicia',
sex: 'female',
children: undefined
}
]
},
{
name: 'Apu',
sex: 'male',
children: [
{
name: 'Josiah',
sex: 'male',
children: []
}
]
}
]
},
{
name: 'Jennifer',
sex: 'female',
children: [
{
name: 'Josh',
sex: 'male',
children: [
{
name: 'Erica',
sex: 'female',
children: [
{
name: 'Alex',
sex: 'male',
children: []
}
]
}
]
},
{
name: 'Laura',
sex: 'female',
children: []
},
{
name: 'Jordan',
sex: 'male',
children: [
{
name: 'Ezekiel',
sex: 'male',
children: []
}
]
}
]
}
]
const thing = filterDeep(people);
console.log('wut',thing)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment