1. /**
  2. * Matches patterns of arrays without async logic.
  3. *
  4. * @param {Array<Array<any>>} patterns - An array of arrays representing the patterns to match.
  5. * @param {Array<any>} data - The array to search for the patterns in.
  6. * @returns {Array<number>} An array of indices where the patterns are found in the data.
  7. */
  8. function matchPatterns(patterns, data) {
  9. const matches = [];
  10. for (let i = 0; i < data.length; i++) {
  11. for (const pattern of patterns) {
  12. if (arraysMatch(data[i], pattern)) {
  13. matches.push(i); // Add the index if the pattern matches
  14. break; // Stop checking other patterns if a match is found
  15. }
  16. }
  17. }
  18. return matches;
  19. /**
  20. * Checks if two arrays match.
  21. * @param {Array<any>} arr1 - The first array.
  22. * @param {Array<any>} arr2 - The second array.
  23. * @returns {boolean} True if the arrays match, false otherwise.
  24. */
  25. function arraysMatch(arr1, arr2) {
  26. if (arr1.length !== arr2.length) {
  27. return false; // Arrays must have the same length to match
  28. }
  29. for (let i = 0; i < arr1.length; i++) {
  30. if (arr1[i] !== arr2[i]) {
  31. return false; // Elements at corresponding indices must be equal
  32. }
  33. }
  34. return true; // All elements match
  35. }
  36. }

Add your comment