I'm retrieving data from my API using Angular and adding the returned data to $scope.addresses I can get the first item in the array returned and assign it to a var firstAddress = $scope.addresses[0] how can I get the last item in the array returned?
Asked
Active
Viewed 1.5k times
3
moh_abk
- 2,064
- 7
- 36
- 65
-
1Possible duplicate of [Get the last item in an array](http://stackoverflow.com/questions/3216013/get-the-last-item-in-an-array) – Durgpal Singh Jun 09 '16 at 11:02
2 Answers
7
You can do it like this:
var lastAddress = $scope.addresses[$scope.addresses.length - 1]
Léo
- 289
- 1
- 6
0
the idea is to reverse with the function reverse() the array firstly and secondly get the first element
example :
aRray: any[] = ['noredine', 'bahri', 'maroc']; // => array
newArray: any[] = this.aRray.reverse(); // => [ 'maroc', 'bahri','noredine']
console.log(this.newArray[0]); // result => maroc
bahri noredine
- 731
- 9
- 15