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"]
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Answer from Stuart Kershaw on Stack OverflowArray.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"]
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Use splice(startPosition, deleteCount)
array.splice(-1)
Show code snippet
var array = ['abc','def','ghi','123'];
var removed = array.splice(-1); //last item
console.log( 'array:', array );
console.log( 'removed:', removed );
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Is there an easy and elegant way of removing the last element of an array?
Why last node remove operation in array have O(n)? - Data Structures - Code with Mosh Forum
Removal of the last element of an array
How does an ArrayList remove an element from an array?
Edit: Solved
It's much more flexible to use generic lists instead of arrays. Arrays are immutable and should not be used when there is a need to add or remove elements. Another option is to use array lists, but others reported that they are deprecated and that generic lists should be used instead.
Thank you all for the help!
-------------
PowerShell 7, an array like $array = @()
Like the title say - is there?
The solutions I've found online are all wrong.
- Array slicing
$array = $array[0..($array.Length - 2)]
This does not work if the array length is 1, because it resolves to $array[0..-1]. Step-by-step debugging shows that instead of deleting the last remaining element of the array, it will duplicate that element. The result will be an array of 2 elements, not 0.
- Select-Object
$array = $array | Select-Object -SkipLast 1
This does not work well with Hashtables as array elements. If your array elements are Hashtables, it will convert them to System.Collections.Hashtable. Hashtable ($example = @{}) and System.Collection.Hashtable are not the same type and operations on those two types are different (with different results).
Edit for the above: There was a typo in that part of my code and it returned some nonsense results. My bad.
- System.Collections.ArrayList
Yes, you can convert an array to System.Collection.ArrayList, but you are then working with System.Collections.ArrayList, not with an array ($array = @()).
----------------
One solution to all of this is to ask if the array length is greater than one, and handle arrays of 1 and 0 elements separately. It's using an if statement to simply remove the last element of an array, which is really bad.
Another solution is to loop through an array manually and create a new one while excluding the last element.
And the last solution that I've found is not to use arrays at all and use generic lists or array lists instead.
Is one of these options really the only solution or is there something that I'm missing?