How to get the last element in a list?
Hello, guys long time no write :D, I have come to a rather stupid question, but I believe you can help me with it.
Hello, guys long time no write :D, I have come to a rather stupid question, but I believe you can help me with it.
If you want to get the last item from the list and remove it from the List, you can use the pop() method.
list = []
list.push("abc")
list.push("123")
list.push("xyz")
print( list.pop() ) // >> "xyz"
print( list.last() )
print( list.pop() ) // >> "123"
print( list.last() )
print( list.pop() ) // >> "abc"
print( list.last() ) // >> "???"
print( list.pop() ) // >> "???"
If you want to get it and leave it on the list,
then
item = list[ list.length - 1 ]
You can also add a new method to the list class.
List.last = function()
return this[ this.length - 1]
end
If you want, you can also write your own external function that will perform some specific operations on the list and this element.
getLast = function( list )
return list[list.length-1] +1
end
Ooookey, thanks!