Jha Nitesh
Jha Nitesh

Reputation: 388

How to get the last element inside array using VTL?

I am writing AWS AppSync resolver. Where I do need to get the last element in my array. AWS AppSync supports VTL language.

Example:

#set($items=["color", "taste", "shape"])

#set($result="shape")

I am using $array.size() but didn't work. I don't see any option in Utility Helpers. .

#set($result=$item[$item.size()-1])

Thanks,

Upvotes: 0

Views: 2190

Answers (1)

Jha Nitesh
Jha Nitesh

Reputation: 388

The solution is to make sure you are making the arithmetic operation inside -> set($result=$item[$item.size()-1]);

Set the array size into a separate variable and use another variable to get the last index.

#set($length=$facts.size())

#set($lastIndex = $length - 1)

#set($fact=$facts.get($lastIndex))

Upvotes: 2

Related Questions