3

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?

1

2 Answers 2

7

You can do it like this:

var lastAddress = $scope.addresses[$scope.addresses.length - 1]
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.