What is the method of continuous filtering of js array
This article mainly explains the "js array continuous screening processing method is what", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "js array continuous filtering processing method is what" it!
Algorithm interview questions:
There is an array [1, 1, 2, 3, 4, 5, 5, 8, 10, 22, 24, 25, 26, 66]. Please write a way to turn the array into [1, 1, 2, 3, 4, 5], 8, 10, 22, [24, 25, 26], 66].
The general idea is loop traversal, each traversal to determine whether the difference between the current item and the previous item is 1, here we need to consider if the result is 1, how to deal with, the result is not 1 how to deal with.
The result is not 1, we can insert the current item directly into the result array, but here we need to consider the boundary problem, we set two variables, the first variable array length len, the second variable array traversal start position I, for convenience, we set I to 1.
The code is as follows:
Var len = arr.length
Var res = []
For (var I = 1; I