Created
May 10, 2016 01:59
-
-
Save makshark/5be5520b3304e0141796443543c5ecb2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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