Last active
December 8, 2015 09:43
-
-
Save mildfuzz/17813dd333a30bdaf884 to your computer and use it in GitHub Desktop.
arrayContainsSub matches arrays within arrays
This file contains hidden or 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
| function arrayContainsSub(arr, sub) { | |
| var first = sub[0], | |
| i = 0, | |
| starts = []; | |
| while (arr.indexOf(first, i) >= 0) { | |
| starts.push(arr.indexOf(first, i)); | |
| i = arr.indexOf(first, i) + 1; | |
| } | |
| return !!starts | |
| .map(function(start) { | |
| for (var i = start, j = 0; j < sub.length; i++, j++) { | |
| if (arr[i] !== sub[j]) { | |
| return false; | |
| } | |
| if (j === sub.length - 1 && arr[i] === sub[j]) { | |
| return true; | |
| } | |
| }; | |
| }).filter(function(res) { | |
| return res; | |
| }).length; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment