Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push Functions into an Array - Loop through and Splice?

Using Javascript i need to be able to:

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

2: Then run each function one by one (for this example just an alert of the parameter/number)

3: After each function i need to be able to SPLICE that function out of the array

4: Check the Array Length after everytime - Once the array is empty again - alert the user it is complete

Now i seem to be able to do task 1,2 and 4 but i am sturggling with how to splice out the function from the array after it has run - can anyone help? As i cannot remove the function i am never getting the 'done' alert once all functions have been called

My javascript code so far is:

// 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);
}
like image 248
Lauren Reynolds Avatar asked Sep 24 '12 19:09

Lauren Reynolds


People also ask

How do you push a function into an array?

Array.prototype.push() The push() method adds one or more elements to the end of an array and returns the new length of the array.

How do you push an array into an array?

Use the concat function, like so: var arrayA = [1, 2]; var arrayB = [3, 4]; var newArray = arrayA. concat(arrayB); The value of newArray will be [1, 2, 3, 4] ( arrayA and arrayB remain unchanged; concat creates and returns a new array for the result).

How do you push an element to an array of objects?

The push() method is used to add one or multiple elements to the end of an array. It returns the new length of the array formed. An object can be inserted by passing the object as a parameter to this method. The object is hence added to the end of the array.


1 Answers

First of all you are not pushing functions into the array at the moment, you execute the func instead. To achieve the push your func 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 could simply iterate over the array

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

If we are dealing with asynchronous functions then they need to have a callback:

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

And then you can either use a counter to run in parallel.

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

Or in sequence:

function run(){
  var fn = arr.shift();
  if(!fn){
    console.log('Done');
  } else {
    fn(run);
  }
}
run();
like image 145
DeadAlready Avatar answered Nov 03 '22 01:11

DeadAlready