/**
* Matches patterns of arrays without async logic.
*
* @param {Array<Array<any>>} patterns - An array of arrays representing the patterns to match.
* @param {Array<any>} data - The array to search for the patterns in.
* @returns {Array<number>} An array of indices where the patterns are found in the data.
*/
function matchPatterns(patterns, data) {
const matches = [];
for (let i = 0; i < data.length; i++) {
for (const pattern of patterns) {
if (arraysMatch(data[i], pattern)) {
matches.push(i); // Add the index if the pattern matches
break; // Stop checking other patterns if a match is found
}
}
}
return matches;
/**
* Checks if two arrays match.
* @param {Array<any>} arr1 - The first array.
* @param {Array<any>} arr2 - The second array.
* @returns {boolean} True if the arrays match, false otherwise.
*/
function arraysMatch(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false; // Arrays must have the same length to match
}
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false; // Elements at corresponding indices must be equal
}
}
return true; // All elements match
}
}
Add your comment