Reputation: 12092
Trying to understand Array.splice().
deleteCount: An integer indicating the number of old array elements to remove.
Ok. Seems straightforward. I want to remove the last 4 objects in the array. I hope that's the same as elements?
arr.splice(<start>, deleteCount<how-many-to-remove>):
// {0 - 8} is an example of object position
let obArr = [{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}]
// Start from the last and remove four:
obArr.splice(-1, 4)
console.log(obArr) // not expected.
console.log(obArr) // expected: [{0}, {1}, {2}, {3}]
Upvotes: 0
Views: 46
Reputation: 92481
Your code starts at the last item and tries to remove four values after the last item. But there aren't four values after the last item. If you want to remove four from the end start earlier in the array:
let obArr = [0, 1, 2, 3, 4, 5, 6, 7]
// Start from the 4th last and remove four:
obArr.splice(-4, 4)
console.log(obArr)
Upvotes: 5