0

I have an array of numbers and want to determine the starting number and ending number of any numbers that are consecutive. The numbers will always be in ascending order.

I would like to create a function that outputs an array which will look like these examples:

[1,2,3,5,6,7] = [[1,3],[5,7]]

[1,2,4,6,7,8,9,10] = [[1,2],[4],[6,10]]

I have made a few meager attempts but I'm just having difficulty with it for whatever reason. I have attempted to adapt this post with no success.

3
  • This is not a GIS question, you are better off searching on stack overflow.
    – Hornbydd
    Commented Nov 22, 2022 at 22:35
  • 1
    How is this not a GIS question? It’s an ArcGIS Arcade AGOL expression question which is directly related to GIS.
    – PMK
    Commented Nov 22, 2022 at 23:37
  • 1
    I understand both sides of the comments. On one hand this is about Arcade, which only exists in ArcGIS software, which is GIS software. On the other hand, Arcade is just a JavasScript derivative, and this question doesn't include an inherently geospatial component.
    – bixb0012
    Commented Nov 23, 2022 at 1:52

1 Answer 1

5

Arcade is heavily influenced by JavaScript, so it is good to look to JavaScript examples when trying to figure out Arcade issues that are non-geospatial in nature. There might be a similar JavaScript example over at StackExchange/StackOverflow, but coming up with a code example was just as quick as searching around those sites.

The following code in Playground | ArcGIS Arcade | ArcGIS Developers,

var arr = [1,2,4,6,7,8,9,10];

var narr = [];
var sarr = [arr[0]];
for(var idx=1; idx < count(arr); idx++) {
    if(arr[idx] == arr[idx-1]+ 1){
        continue;
    } else {
        if(sarr[0] != arr[idx-1]) {
            Insert(sarr, count(sarr), arr[idx-1]);
        }
        Insert(narr, count(narr), sarr);
        var sarr = [arr[idx]];
    }
}
if(sarr[0] != arr[idx-1]) {
    Insert(sarr, count(sarr), arr[idx-1]);
}
Insert(narr, count(narr), sarr);
return narr;

yields,

    [ Array (2) , Array (1) , Array (2) ]

which expands to,

[ [1,2] , [4] , [6,10] ]
1
  • That's exactly what I was looking for. Thank you!
    – PMK
    Commented Nov 23, 2022 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.