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"]
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"]
Use splice(startPosition, deleteCount)
array.splice(-1)
var array = ['abc','def','ghi','123'];
var removed = array.splice(-1); //last item
console.log( 'array:', array );
console.log( 'removed:', removed );
Is there an easy and elegant way of removing the last element of an array?
javascript - Remove the last item from an array (and not return item) - Stack Overflow
[Javascript] Remove last 2 elements of array? slice/splice not working
Why is removing a specific element from an array so needlessly complicated in Javascript?
Videos
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?
pop() function also removes last element from array, so this is what you want(Demo on JSFiddle):
var abc = ['a', 'b', 'c', 'd'];
abc.pop()
alert(abc); // a, b, c
Do this
abc = abc.splice(0, abc.length-1)
Edit: It has been pointed out that this actually returns a new array(albeit with the same variable name).
If you want to return the same array, you'll have to make your own function
function popper(arr) {
arr.pop();
return arr;
}