JavaScript如何检查数组中是否存在连续指定顺序的多个元素
Hey there! Let's sort out this problem—you're right that Array.includes() won't work here, and I'll break down why first, then show you exactly how to fix it.
Why Your Original Code Fails
Your current code uses num.includes(23,34,45)—but here's the thing: the includes() method only takes two arguments at most: the value to search for, and an optional starting index. So your code is actually checking if 23 exists in the array starting at index 34 (which is way beyond your array's length), completely ignoring 34 and 45 as separate values. That's why it doesn't do what you need—it can't check for consecutive elements in order.
The Solution: A Custom Sequence Check
We need to write a function that iterates through the array and checks if the target sequence appears as consecutive elements in the exact order. Here's a straightforward implementation tailored for 3-element sequences:
function hasConsecutiveTriple(arr, targetTriple) { // We only loop up to the point where there's space left for the 3-element sequence for (let i = 0; i <= arr.length - 3; i++) { // Check if current index and next two match the target sequence if (arr[i] === targetTriple[0] && arr[i+1] === targetTriple[1] && arr[i+2] === targetTriple[2]) { return true; } } // No match found after full loop return false; } // Test with your example array const num = [12,23,34,45,56,67,78,89,90]; if (hasConsecutiveTriple(num, [23,34,45])) { console.log('found'); // This will log 'found' since the sequence is consecutive } else { console.log('not found'); } // Test the case that should return false const testArray = [12,23,45,34]; console.log(hasConsecutiveTriple(testArray, [23,34,45])); // Logs false, which is correct
A More Flexible Version (Works for Any Sequence Length)
If you might need to check sequences longer or shorter than 3 elements later, here's a generalized version:
function hasConsecutiveSequence(arr, sequence) { const seqLength = sequence.length; // Early exit if sequence is longer than the array itself if (seqLength > arr.length) return false; for (let i = 0; i <= arr.length - seqLength; i++) { // Use every() to verify all elements in the sequence match consecutive elements in arr const isMatch = sequence.every((val, index) => val === arr[i + index]); if (isMatch) return true; } return false; } // Use it the same way: console.log(hasConsecutiveSequence(num, [23,34,45])); // true console.log(hasConsecutiveSequence(testArray, [23,34,45])); // false
How This Works
- The loop runs from the start of the array to the last index where the target sequence can still fit (e.g., for a 3-element sequence, we stop at
arr.length - 3to avoid out-of-bounds errors when checkingi+2). - For each starting index, we compare consecutive elements in the original array to the target sequence. If every element matches, we return
trueimmediately to save computation. - If we finish the loop without finding a match, we return
false.
This approach is efficient and exactly solves your problem of checking for consecutive elements in a specific order.
内容的提问来源于stack exchange,提问作者Tom S

