Javascript Array of Functions
var array_of_functions = [
first_function,
second_function,
third_function,
forth_function
]
and then when you want to execute a given function in the array:
array_of_functions[0]('a string');
In JavaScript, how to execute next function from an array of functions
Unless func1
closes over funcArray
, you cannot have it reach out and find func2
and execute it, nor should you. Even if func1
does close over funcArray
, it would be poor separation of concerns for func1
to reach out and find itself in funcArray
and then execute func2
.
Instead, have other code that's in charge of running the functions.
If they're synchronous
If the functions complete their work synchronously, then it's simply:
funcArray.forEach(fn => fn());
or
for (const fn of funcArray) {
fn();
}
or if the result of one function should be passed to the next, you can use reduce
:
const finalResult = funcArray.reduce((previousResult, fn) => fn(previousResult), undefined);
...where undefined
is the value to pass to func1
.
If they're asynchronous
If they don't do their work synchronously, you'll need to provide them a way to notify their caller that they've completed their work. Promises are a good, standard way to do that, but you could use simple callbacks instead.
If you make them return promises, for instance, you can use the old promise reduce
trick:
funcArray.reduce((p, fn) => {
return p.then(() => {
fn();
});
}, Promise.resolve());
or if the result of one function should be passed to the next:
funcArray.reduce((p, fn) => {
return p.then(fn);
}, Promise.resolve());
You can provide an argument to Promise.resolve
to set the value to pass to func1
(without one, it'll receive undefined
).
Javascript - Array of functions is this bad practices
In JavaScript a function is a first class citizen, a special kind of object to be precise, so it's perfectly good practice to have an array of functions.
array of arrays in js function
You need to pass array with the bracket [].
function intersection(arrays) {
return arrays;
}
console.log(intersection([[5, 10, 15, 20],[15, 88, 1, 5, 7],[1, 10, 15, 5, 20]]));
Write a function that takes an array by calling another function to return a value and store it on a new array
You can simply push callback return into array and return that same array from your custom fucntion.
You can also modify original array using index.
const customMap = (arr, callback) => {
const finalArr = []
arr.forEach((item, index) => {
const res = callback(item)
finalArr.push(res)
arr[index] = res // This will modify original array
})
return finalArr
}
const numbers = [1, 2, 3, 4, 5];
const newNumbers = customMap(numbers, (number) => {
return number + 3;
});
console.log(newNumbers);
console.log(numbers);
How to convert Javascript Array to array of functions?
I'm not exactly sure about their intention, but a simple approach to provide the given results would be as follows:
function transform(a){
return a.map( (el) => () => el );
}
PS: the interesting follow up question would then be about the differences between the results for b[0]()
and b[1]()
compared to b[2]()
and how to mitigate the problems that might arise for the later.
Related Topics
How to Remove Element from an Array in JavaScript
Calling Setstate in a Loop Only Updates State 1 Time
Is a JavaScript Array Index a String or an Integer
How to Get the Difference of Two Dates in Mm-Dd-Hh Format in JavaScript
How to Clone a JavaScript Object Except for One Key
Fastest Way to Check a String Contain Another Substring in JavaScript
How to Overcome the Cors Issue in Reactjs
Sum of Array Object Property Values in New Array of Objects in JavaScript
Classical Inheritance VS Prototypal Inheritance in JavaScript
Node.Js Tail-Call Optimization: Possible or Not
How to Print a Stack Trace in Node.Js
How to Check Whether an Object Is a Date
How to Check If String Contains Substring
JavaScript Number Split into Individual Digits
Testing If a Checkbox Is Checked with Jquery
Convert Object Array to Hash Map, Indexed by an Attribute Value of the Object
What Does ${} (Dollar Sign and Curly Braces) Mean in a String in JavaScript