Created
September 14, 2023 09:42
-
-
Save MarketingPip/aa8b2bb31aa9a36d96dd3f8ba38a3424 to your computer and use it in GitHub Desktop.
A JavaScript function to call multiple functions and return all results as one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function callAllFunctions(functionsObj) { | |
const resultsArr = []; | |
// Loop through each key-value pair in the object | |
for (let key in functionsObj) { | |
if (typeof functionsObj[key] === 'function') { | |
const result = functionsObj[key](); // Call the current function | |
resultsArr.push(result); // Add the result to array | |
} | |
} | |
return resultsArr; | |
} | |
const exampleFunctions = { | |
hello: function() { return ["Hello"] }, | |
world: function() { return ["World"] } | |
}; | |
console.log(...callAllFunctions(exampleFunctions)); | |
/* Output: | |
["Hello"] | |
["World"] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment