Remove first Item of the array (like popping from stack)
Plunker
$scope.remove = function(item) {
$scope.cards.splice(0, 1);
}
Made changes to .. now it will remove from the top
Just use arr.slice(startingIndex, endingIndex)
.
If you do not specify the endingIndex
, it returns all the items starting from the index provided.
In your case arr=arr.slice(1)
.
The easiest way is using shift()
. If you have an array, the shift
function shifts everything to the left.
var arr = [1, 2, 3, 4];
var theRemovedElement = arr.shift(); // theRemovedElement == 1
console.log(arr); // [2, 3, 4]
const a = [1, 2, 3]; // -> [2, 3]
// Mutable solutions: update array 'a', 'c' will contain the removed item
const c = a.shift(); // prefered mutable way
const [c] = a.splice(0, 1);
// Immutable solutions: create new array 'b' and leave array 'a' untouched
const b = a.slice(1); // prefered immutable way
const b = a.filter((_, i) => i > 0);
const [c, ...b] = a; // c: the removed item