You need to handover the array after shifting an item.
Array#shift returns/removes the item at index zero or undefined if the array is empty.
function del(arr) {
if (arr.length === 0) return arr;
arr.shift();
return del(arr);
}
const arr = [1, 2, 3, 4]; // keep the variable/omit reassignment
// just to give proof
console.log(del(arr) === arr); // same object reference
console.log(arr); // empty array
Answer from Nina Scholz on Stack OverflowYou need to handover the array after shifting an item.
Array#shift returns/removes the item at index zero or undefined if the array is empty.
function del(arr) {
if (arr.length === 0) return arr;
arr.shift();
return del(arr);
}
const arr = [1, 2, 3, 4]; // keep the variable/omit reassignment
// just to give proof
console.log(del(arr) === arr); // same object reference
console.log(arr); // empty array
If all you're trying to do is empty the array, you can do:
let arr = [1, 2, 3, 4];
arr.splice(0, arr.length);
console.log(arr);
change the function as below:
var waypoints = [1,2,3,4,5,6,7,8,9,0];
var deleteWaypoint = (waypoints)=>{
if (this.first){
this.first = false;
waypoints.shift();
return waypoints
} else {
this.first = true;
waypoints.pop()
return waypoints
}
}
console.log("length before " + waypoints.length)
waypoints = deleteWaypoint(waypoints)
console.log("length after " + waypoints.length)
This works perfectly fine for me;
var deleteWaypoint = function (waypoints) {
if (this.first) {
this.first = false;
waypoints.shift()
} else {
this.first = true;
waypoints.pop()
}
};
this.first = true
var waypoints = [1,2,3,4,5];
console.log("length before " + waypoints.length);
deleteWaypoint(waypoints)
console.log("length after " + waypoints.length);
Array.shift() returns the removed array, in below line copied from code posted in question,
waypoints = this.deleteWaypoint(waypoints)
the reference of array is getting re-assigned to the removed element which is not array and does not have length property and hence returning undefined
Ways to clear an existing array A:
Method 1
(this was my original answer to the question)
A = [];
This code will set the variable A to a new empty array. This is perfect if you don't have references to the original array A anywhere else because this actually creates a brand new (empty) array. You should be careful with this method because if you have referenced this array from another variable or property, the original array will remain unchanged. Only use this if you only reference the array by its original variable A.
This is also the fastest solution.
This code sample shows the issue you can encounter when using this method:
var arr1 = ['a','b','c','d','e','f'];
var arr2 = arr1; // Reference arr1 by another variable
arr1 = [];
console.log(arr2); // Output ['a','b','c','d','e','f']
Method 2 (as suggested by Matthew Crumley)
A.length = 0
This will clear the existing array by setting its length to 0. It also works when using "strict mode" in ECMAScript 5 because the length property of an array is a read/write property.
Method 3 (as suggested by Anthony)
A.splice(0,A.length)
Using .splice() will work perfectly, but since the .splice() function will return an array with all the removed items, it will actually return a copy of the original array. Benchmarks suggest that this has no effect on performance whatsoever.
Method 4 (as suggested by tanguy_k)
while(A.length > 0) {
A.pop();
}
This solution is not very succinct, and it is also the slowest solution, contrary to earlier benchmarks referenced in the original answer.
Performance
Of all the methods of clearing an existing array, methods 2 and 3 are very similar in performance and are a lot faster than method 4. See this benchmark.
As pointed out by Diadistis in their answer below, the original benchmarks that were used to determine the performance of the four methods described above were flawed. The original benchmark reused the cleared array so the second iteration was clearing an array that was already empty.
The following benchmark fixes this flaw: http://jsben.ch/#/hyj65. It clearly shows that methods #2 (length property) and #3 (splice) are the fastest (not counting method #1 which doesn't change the original array).
This has been a hot topic and the cause of a lot of controversy. There are actually many correct answers and because this answer has been marked as the accepted answer for a very long time, I will include all of the methods here.
If you need to keep the original array because you have other references to it that should be updated too, you can clear it without creating a new array by setting its length to zero:
A.length = 0;
So you're basically asking what shift does and here you go:
What you can do using for(var i=0;... you can do using shift() (quite similar but not!)
Using for loop (and index)
var array = [
function(){return "a";},
function(){return "b";}
];
for(var i=0; i<array.length; i++){
console.log( array[i]() );
// "a"
// "b"
}
console.log(array.length); //2 !!Still there!!!
Using shift() (and while for example)
var array = [
function(){return "a";},
function(){return "b";}
];
while(array.length){ // while array has length
console.log( array.shift()() ); // execute and remove from array
// "a"
// "b"
}
console.log(array.length); //0 !!!Empty array due to shift()!!!
So basically it removes a key from your Array and returns it.
As long as that array has keys it will loop until it's empty.
The difference between the two is drastic:
The for loop in example 1. will loop but not alter your original array.
Using shift() in example 2. you're (using and) removing your Array keys one by one.
Read more about Array manipulation:
Array.prototype.shift 1 <-- [2,3,4]
Array.prototype.unshift 5 --> [5,2,3,4]
Array.prototype.push [5,2,3,4,6] <-- 6
Array.prototype.pop [5,2,3,4] --> 6
and other Methods
You can simplify the logic using Array.reduce
Here how you can do that:
var puzzlers = [
function(a) { return 8 * a - 10; },
function(a) { return (a - 3) * (a - 3) * (a - 3); },
function(a) { return a * a + 4; },
function(a) { return a % 5; }
];
function runPuzzler(inputValue){
return puzzlers.reduce(function(prev, curr){
return curr(prev);
},inputValue);
}
Output :
EachArrayValues::100
EachArrayValues::200
EachArrayValues::0
EachArrayValues::400
EachArrayValues::500
EachArrayValues::-50
Negative value found, ie -50