Skip to content

Instantly share code, notes, and snippets.

@makshark
Created May 10, 2016 01:59
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 makshark/5be5520b3304e0141796443543c5ecb2 to your computer and use it in GitHub Desktop.
Save makshark/5be5520b3304e0141796443543c5ecb2 to your computer and use it in GitHub Desktop.
// 1.2 Makovskyi
function search(needle, haystack) {
var i = 0;
var valueSearching = false;
if (typeof haystack === 'object') {
if (Array.isArray(haystack)) {
for (; i < haystack.length; i++) {
if (search(needle, haystack[i])) {
return true;
}
}
} else {
Object.keys(haystack).some(function object(key) {
if (search(needle, haystack[key])) {
valueSearching = true;
return true;
}
return false;
});
return valueSearching;
}
} else {
if (needle === haystack) {
return true;
}
}
return false;
}
console.log(search(777, { a: [1, 2, { s: 4, c: { u: 5, mm: [{ findMe: 777 }, 7, 8] } }], s: 9 }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment