Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove last item from array

I have the following array.

var arr = [1,0,2]; 

I would like to remove the last element i.e. 2.

I used arr.slice(-1); but it doesn't remove the value.

like image 226
Prithviraj Mitra Avatar asked Oct 23 '13 14:10

Prithviraj Mitra


People also ask

How can I remove the last item in an array?

The pop() method removes (pops) the last element of an array. The pop() method changes the original array. The pop() method returns the removed element.

What removes the last element of an array and return it?

JavaScript pop() Method: The pop() method is a built-in method that removes the last element from an array and returns that element. This method reduces the length of the array by 1. Syntax: array.

How do you remove the last object of an array in Java?

We can use the remove() method of ArrayList container in Java to remove the last element. ArrayList provides two overloaded remove() method: remove(int index) : Accept index of the object to be removed. We can pass the last elements index to the remove() method to delete the last element.

How do you delete the last element of an array in Python?

Using del The del operator deletes the element at the specified index location from the list. To delete the last element, we can use the negative index -1. The use of the negative index allows us to delete the last element, even without calculating the length of the list.


1 Answers

Array.prototype.pop() by JavaScript convention.

let fruit = ['apple', 'orange', 'banana', 'tomato']; let popped = fruit.pop();  console.log(popped); // "tomato" console.log(fruit); // ["apple", "orange", "banana"] 
like image 156
Stuart Kershaw Avatar answered Oct 08 '22 04:10

Stuart Kershaw