admin 管理员组文章数量: 1086019
Let's say I have this function
function myFunction(a, b, c) {
... code here ...
}
Now I want to call it multiple times with several different arguments each time:
myFunction('sky', 'blue', 'air');
myFunction('tree', 'green', 'leaf');
myFunction('sun', 'yellow', 'light');
myFunction('fire', 'orange', 'heat');
myFunction('night', 'black', 'cold');
How can I merge all those calls in just one?
I know how to do it with iterations or forEach
when there is just one argument, but I can't figure out how to do it with various non-numerical arguments.
Let's say I have this function
function myFunction(a, b, c) {
... code here ...
}
Now I want to call it multiple times with several different arguments each time:
myFunction('sky', 'blue', 'air');
myFunction('tree', 'green', 'leaf');
myFunction('sun', 'yellow', 'light');
myFunction('fire', 'orange', 'heat');
myFunction('night', 'black', 'cold');
How can I merge all those calls in just one?
I know how to do it with iterations or forEach
when there is just one argument, but I can't figure out how to do it with various non-numerical arguments.
- 1 That depends on your parameters, are they stored in an array? are they based on user input? Can you give some more context? – roberrrt-s Commented Sep 12, 2016 at 13:47
-
1
"How can I merge all those calls in just one?" You can't, if you mean a single call to
myFunction
, not without changing howmyFunction
works. Depending on where the values e from, you can certainly use a loop to only code a single call tomyFunction
. – T.J. Crowder Commented Sep 12, 2016 at 13:48
3 Answers
Reset to default 5Unless you change myFunction
, you can't call it once and have it magically act as though it had been called five times.
You can code a single call in a loop, though, if the information you're for the arguments using is stored elsewhere.
For instance, if we assume an array of objets:
var data = [
{a: 'sky', b: 'blue', c: 'air'},
{a: 'tree', b: 'green', c: 'leaf'},
{a: 'sun', b: 'yellow', c: 'light'},
{a: 'fire', b: 'orange', c: 'heat'},
{a: 'night', b: 'black', c: 'cold'}
]:
then
data.forEach(function(entry) {
myFunction(entry.a, entry.b, entry.c);
});
Or if it's an array of arrays, we can use the nifty Function#apply
function:
var data = [
['sky', 'blue', 'air'],
['tree', 'green', 'leaf'],
['sun', 'yellow', 'light'],
['fire', 'orange', 'heat'],
['night', 'black', 'cold']
]:
then:
data.forEach(function(entry) {
myFunction.apply(null, entry);
});
You seem to be looking for apply
var values = [
['sky', 'blue', 'air'],
['tree', 'green', 'leaf'],
['sun', 'yellow', 'light'],
['fire', 'orange', 'heat'],
['night', 'black', 'cold']
];
values.forEach(function(args) { myFunction.apply(null, args); })
or spread syntax in ES6:
for (const args of values) myFunction(...args);
lets suppose you already have an array with all the values:
var colors = ['sky', 'blue', 'air', 'tree', 'green', 'leaf', 'sun', 'yellow', 'light', 'fire', 'orange', 'heat', 'night', 'black', 'cold'];
while(colors.length > 0){
myFunction.apply(this, colors.splice(0,3)); //call the function with chunks of 3 elements
}
本文标签: Call a function multiple times with more than one argument in JavaScriptStack Overflow
版权声明:本文标题:Call a function multiple times with more than one argument in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744094779a2532695.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论