Skip to content

Instantly share code, notes, and snippets.

@MarketingPip
Created September 14, 2023 09:42
Show Gist options
  • Save MarketingPip/aa8b2bb31aa9a36d96dd3f8ba38a3424 to your computer and use it in GitHub Desktop.
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.
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