Skip to content

Instantly share code, notes, and snippets.

@quackingduck
Forked from TimurM/nestedListMatch - Recursive
Last active August 29, 2015 14:25
Show Gist options
  • Save quackingduck/b9ca98bad23194144a1d to your computer and use it in GitHub Desktop.
Save quackingduck/b9ca98bad23194144a1d 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