Pushing functions into an array - loop and splicing?

Using Javascript, I should be able to:

1: Push a certain amount of the same function (with a different parameter in each) into the array.

2: Then run each function one at a time (for this example, just a warning about the parameter / number)

3: After each function, I should be able to SPLICE, which works from an array

4: Check the length of the array after each time - after the array is empty again - warn the user about completion

Now I seem to be able to complete tasks 1,2 and 4, but I turn around how to combine a function from an array after it starts - can anyone help? Since I cannot delete a function, I never get a "done" warning after all functions have been called

My javascript code so far:

// Create empty array var array = []; // Push functions into array - dynamic amount and could be any amount of functions array.push(func(1)); array.push(func(2)); array.push(func(3)); // Call array manager function after pushing array arrayManager(); // Array manager function to splice and detect when finished function arrayManager() { if (array.length < 1) { alert("done"); } else { ////////////////////////////////// // << THIS IS WHERE I DON'T KNOW HOW TO SPLICE THE ITEM FROM THE ARRAY ////////////////////////////////// } } // Function for array objects - alert passed parameter function func(num){ alert(num); } 
+4
source share
5 answers

First of all, you are not pushing functions in the array at the moment, instead you are executing func. To get the push, your function should look like this:

 // Function for array objects - alert passed parameter function func(num){ return function(){ alert(num); } } 

Now, if your functions are synchronous, you can just iterate over the array

 for(var i in arr){ arr[i](); } console.log('done'); 

If we are dealing with asynchronous functions, then they must have a callback:

 // Function for array objects - alert passed parameter function func(num){ return function(callback){ alert(num); callback(); } } 

And then you can use the counter for parallel operation.

 var count = arr.length; for(var i in arr){ arr[i](function(){ if(--count === 0){ console.log('Done'); } }); } 

Or sequentially:

 function run(){ var fn = arr.shift(); if(!fn){ console.log('Done'); } else { fn(run); } } run(); 
+14
source

This should do what you are looking for:

 var next_func = array.splice(0, 1)[0] 

"splicing" takes at least two parameters: the first is the index that should start with deletion, and the second is the number of elements to delete. Since it returns a new array, just to get the function, you get the first value from splicing. You can also add as many values ​​as you want after these first two parameters; they will be added to the array instead of what you deleted.

However, I am curious why you are doing it this way - although I can understand that doing something non-traditional is like an intellectual exercise, if you are new to programming, I would say that there is a better way to do this. There is no particular need to remove elements from the array, as you use functions from it. It would be more practical for me to execute each function from an array, and then set "array = []" after the loop finishes. However, depending on the circumstances, this may still be the best way to do what you do, I just thought I was giving food for thought.

+1
source
 // Create empty array var array = []; // Push functions into array - dynamic amount and could be any amount of functions array.push(function() { func(1); }); //if you call array.push(func(1)) the function is executed immediatly //then null is stored in the array array.push(function() { func(2); }); array.push(function() { func(3); }); // Call array manager function after pushing array arrayManager(); // Array manager function to splice and detect when finished function arrayManager() { while (array.length){ var fnc=array.splice(0,1)[0] fnc(); } alert ("done"); } // Function for array objects - alert passed parameter function func(num){ alert(num); }​ 
+1
source

That's what you need?

1: Push a certain amount of the same function (with a different parameter in each) into the array.

 function func(num){ alert(num); } var k = []; k.push({f:func, params:[1]}); k.push({f:func, params:[2]}); k.push({f:func, params:[3]}); 

2: Then run each function one at a time (for this example, just a warning about the parameter / number)

3: After each function, I should be able to SPLICE, which works from an array

4: Check the length of the array after each time - after the array is empty again - warn the user about completion

 while (k.length > 0) { k[0].f(k[0].params); k.shift(); } alert("done"); 
+1
source

http://jsfiddle.net/nZ459/1/

If you want to push each function with its parameters the way you do, you will have to wrap the function as follows:

 function wrapper(arg){ return function(){ console.log(arg); }; } var ar = []; console.log("pushing functions"); ar.push(wrapper(1)); ar.push(wrapper(2)); ar.push(wrapper('three')); console.log("done pushing functions"); while (ar.length){ var fn = ar.shift(); console.log("calling ", fn); fn(); } 

Alternatively, you can create a common wrapper that takes a function, and arguments as parameters, and returns an anonymous function.

To more accurately answer your question, the easiest way to start the array as you wish is as follows:

 while (array.length){ var item = array.shift(); ... } 
0
source

Source: https://habr.com/ru/post/1435998/


All Articles