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?
-
1Possible duplicate of Get the last item in an arrayDurgpal Singh– Durgpal Singh2016-06-09 11:02:13 +00:00Commented Jun 9, 2016 at 11:02
Add a comment
|
2 Answers
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