Skip to content

Instantly share code, notes, and snippets.

@mildfuzz
Last active December 8, 2015 09:43
Show Gist options
  • Select an option

  • Save mildfuzz/17813dd333a30bdaf884 to your computer and use it in GitHub Desktop.

Select an option

Save mildfuzz/17813dd333a30bdaf884 to your computer and use it in GitHub Desktop.
arrayContainsSub matches arrays within arrays
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