-1

I have arrays like this one below.

[1,2,3, 10, 15, 24,25,26,27]

What I want is to filter out the non consecutive numbers excluding the first number in a series but keeping the last in a series like this below.

[2,3, 25,26,27]

I am new to coding and I'm not sure how to write this code yet but I have mocked up pseudocode, would you check this and see if my logic is on track?

Var startArray = [1,2,3, 10, 15, 24,25,26,27];

Var endArray = [ ];

Loop: while there are numbers left in the start array  
GET: The first number of the startArray

IF: the first number is 1
MOVE: to next number in the array

IF: the current number + 1 in the array subtracted by the current number -1 in the array === 2
PUSH: current number into endArray

ELSE IF: the current number -1 ===1
PUSH: current number into endArray

END IF

END LOOP

Thank you for your help.

dnest007
  • 35
  • 7

3 Answers3

2

You will just have to loop over everything and simply check, if the current number is one higher than the previous number. If so, push it to your resulting array:

var startArray = [1, 2, 3, 10, 15, 24, 25, 26, 27];
var endArray = [];

for(var i = 0; i < startArray.length; i++) {
  if(startArray[i] - 1 === startArray[i - 1]) {
    endArray.push(startArray[i]);
  }
}

$.writeln(endArray); // => 2, 3, 25, 26, 27
mdomino
  • 1,195
  • 1
  • 8
  • 22
1

Here is what I came up with.

const start = [1, 2, 3, 10, 15, 24, 25, 26, 27];
let end = new Array(), i = 0;
while (i < start.length) {
  let j = 2;
  if (start[i+j-2] + 1 == start[i+j-1])
    end.push(start[i+j-2], start[i+j-1]);
  else { i++; continue; }
  while (i+j < start.length && start[i+j-1] + 1 == start[i+j])
    end.push(start[i+j++]);
  i += j;
}

Explanation: The first while loop iterates through every sequence of consecutive numbers. The variable j is first used to check the first and the second elements of the expected sequence, and if they are not consecutive, it increments i by 1, changing the start point of the next expected sequence of consecutive numbers. If they are consecutive numbers, it pushes both of them in the end array, and continues with the nested loop, which checks if the j-1 element ( of the current sequence, that always has the first element i ) and the j element are consecutive, and if they are, it adds the j element of the sequence to the end array. And finally, when the nested loops is done executing, it adds j to i, to change the starting point of the next sequence.

I know the explanation is pretty messy, but hopefully it helps you understand.

smunteanu
  • 422
  • 3
  • 9
1

We can use array builtin method filter. Explanation in the comments.

const start = [1, 2, 3, 10, 15, 24, 25, 26, 27]

const result = start.filter((el, i) => {
  // if `el` is the any element in consecutive sequence except the first
  if (el - 1 === start[i - 1]) return true

  // else it is consecutive sequence of size 1 (10, 15) in the example,
  // or it is first element in consecutive sequence of size greater than 1
  return false
})

console.log(result)
Shivam Singla
  • 2,117
  • 1
  • 10
  • 22
  • It does not work though... it does not display 1 and 24 – smunteanu Feb 09 '21 at 12:27
  • 2
    @smunteanu the desired output in your question also doesn't have `1` and `24`. Please make your question clear. Thanks – Shivam Singla Feb 09 '21 at 12:56
  • 2
    `filter()` is not supported in InDesign, as InDesign's ExtendScript is based on a *really* old version of JavaScript. Neither is `console.log()`. – mdomino Feb 09 '21 at 13:34