Skip to content

Instantly share code, notes, and snippets.

@TimurM
Created July 15, 2015 02:37
Show Gist options
  • Save TimurM/af9c0d9f9c2b6cb49bd5 to your computer and use it in GitHub Desktop.
Save TimurM/af9c0d9f9c2b6cb49bd5 to your computer and use it in GitHub Desktop.
var nestedListMatch = function(lists, match) {
var firstItem = lists[0];
if(firstItem === match) {
return match;
}else if (Array.isArray(firstItem)){
var result = nestedListMatch(firstItem, match)
if(result === match) {
return match;
}
}
var newList = lists.slice(1);
return (newList.length>0) ? nestedListMatch(newList, match) : null;
}
var nestedList = [[1,2,[3,4,5]], 6, 9];
console.log(nestedListMatch(nestedList, 2))
console.log(nestedListMatch(nestedList, 5))
console.log(nestedListMatch(nestedList, 9))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment