How to get last x items from an array?

Hello,

Is there a way to get last x values from an array?

Let’s say I have [‘a’,‘b’,‘c’,‘d’,‘e’,‘f’,‘g’].

Getting the first x can be done with array slicing [0:3], but how do I get the last x?

I’ve tried [-1:-3] and [-1:3], but they did not work. Only workaround I’ve found so far is

select [anArray[-1], anArray[-2], anArray[-3]] from aBucket

SELECT [‘a’,‘b’,‘c’,‘d’,‘e’,‘f’,‘g’][-3:];

1 Like

Thank you @geraldss

That was exactly what I was looking for. I did not know the 2nd number is an optional value.

1 Like

Thanks, This works but Couldn’t find this info anywhere. Btw, A blog post from couchbase says both parameter required - https://blog.couchbase.com/working-json-arrays-n1ql/