Skip to content

Instantly share code, notes, and snippets.

@lydemann
Last active December 14, 2021 14:08
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 lydemann/51531e7e3a90f32741e16dba550acfd7 to your computer and use it in GitHub Desktop.
Save lydemann/51531e7e3a90f32741e16dba550acfd7 to your computer and use it in GitHub Desktop.
function containsInNestedObjectBF(obj, val) {
const queue = [obj];
while (queue.length > 0) {
const currentObj = queue.shift();
if (currentObj === val) {
return true;
}
const keys = currentObj instanceof Object ? Object.keys(currentObj) : [];
for (const key of keys) {
const objVal = currentObj[key];
queue.push(objVal);
}
}
return false;
}
var nestedObject = {
data: {
info: {
stuff: {
thing: {
moreStuff: {
something: 'foo2',
someVal: 40
}
}
}
}
}
}
let hasIt = containsInNestedObject(nestedObject, 40); // true
let doesntHaveIt = containsInNestedObject(nestedObject, "foo"); // false
console.log(hasIt, doesntHaveIt);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment