每个数组项是一个函数调用时如何调用每个数组项

时间:2019-06-22 03:58:56

标签: javascript arrays function for-loop foreach

我有一个充满函数调用(即func(param);)的数组,如下所示:

var array = [
  func(param),
  func(param),
  func(param)
];

稍后,我想一次调用所有这些函数调用。我怎样才能做到这一点?

我知道您可以使用for循环或forEach这样在数组的每个项目上运行一个函数:

for (var i = 0; i < 10; i++) {
  // do stuff
}

arrayName.forEach(function(e) {
  // do stuff
});

但是我不确定如何编写代码以便调用它?

我尝试了以下操作,但是出现错误“ arrayName [i]不是函数”。

for (i = 0; i < arrayName.length; i++) {
  arrayName[i]();
}

编辑:三种解决方案

1:BEST,使用while循环

这是基本代码:

var soundsWaitingForNextBar = [];

while (soundsWaitingForNextBar.length > 0) {
  soundsWaitingForNextBar[0](); // call first item in array
  soundsWaitingForNextBar.shift(); // remove first item in array
}

这是一个更深入的版本,其中每秒将项目推入数组,但仅每十秒钟清空一次数组:

var soundsWaitingForNextBar = [];

function foo() {
  console.log('')
} // just making sure foo is a function

// pusing some functions into the array
soundsWaitingForNextBar.push(() => foo());
soundsWaitingForNextBar.push(() => foo());

setInterval(function() { // pushing items to array periodically to make sure the while loop is only emptying when it is called by setInterval and not every time an item is added to the array
  soundsWaitingForNextBar.push(() => foo());
}, 2000);


setInterval(function() {
  // this while loop does the magic: it calls and removes each item in the array
  while (soundsWaitingForNextBar.length > 0) {
    soundsWaitingForNextBar[0](); // call first item in array
    soundsWaitingForNextBar.shift(); // remove first item in array
  }
}, 10000);

setInterval(function() { // just checking to see how/when items are added to the array and emptied
  console.log('array.length: ' + soundsWaitingForNextBar.length);
}, 1000);

2:使用array[0]();来调用每个数组项/函数。

const func = console.log;
const param = 'foo';

const array = [
  () => func(param),
  () => func(param),
  () => func(param)
];

console.log(array.length); // just noting the original array length

arrayLength = array.length;
for (let i = 0; i < arrayLength; i++) {
  array[0]();
  array.shift();
}

console.log(array.length); // just making sure the array is empty

3:使用array.forEach(fn => fn());来调用每个数组项/函数。

const func = console.log;
const param = 'foo';

const array = [
  () => func(param),
  () => func(param),
  () => func(param)
];

console.log(array.length); // just noting the original array length

array.forEach(fn => fn()); // calls each array item/function

arrayLength = array.length;
for (let i = 0; i < arrayLength; i++) {
  array.shift();
}

console.log(array.length); // just making sure the array is empty

注意:在最初用于.splice从数组中删除项目的for循环中,我使用了array.length。例如:for (let i = 0; i < array.length; i++)。这没有用(我不确定为什么),所以我决定使用arrayLength = array.length;在for循环之前确定数组长度。现在,该程序可以按预期工作。

4 个答案:

答案 0 :(得分:2)

您只需要在数组中传递函数名称

var arrayName = [
  func,
  func,
  func
];

function func(a){
return a*2;
};
var arrayName = [
  func,
  func,
  func
];

for (i = 0; i < arrayName.length; i++) {
  console.log(arrayName[i](2));
}

答案 1 :(得分:1)

使用

var array = [
  func(param),
  func(param),
  func(param)
];

您已经已经在声明数组时立即调用这些函数。听起来好像您要推迟执行它们-您希望它们可以在将来的某个时间被调用,而不是现在。而是创建一个函数数组,该函数数组在调用时使用所需的参数来调用该函数,例如:

var array = [
  () => func(param),
  () => func(param),
  () => func(param)
];

然后,您的for循环(或forEach)将起作用:

for (let i = 0; i < array.length; i++) {
  array[i]();
}

演示:

const func = console.log;
const param = 'foo';

const array = [
  () => func(param),
  () => func(param),
  () => func(param)
];
array.forEach(fn => fn());

您也可以使用.bind代替高阶函数:

const func = console.log;
const param = 'foo';

const array = [
  func.bind(undefined, param),
  func.bind(undefined, param),
  func.bind(undefined, param)
];
array.forEach(fn => fn());

答案 2 :(得分:1)

因为当前您正在存储func(param)的返回值。使每一项成为函数引用,以便以后可以调用它:

var array = [
  () => func(param),
  () => func(param),
  () => func(param)
];

演示:

function func(p) {
  console.log(p);
}

let param = "Parameter";

var array = [
  () => func(param),
  () => func(param),
  () => func(param)
];

array.forEach(e => e());

答案 3 :(得分:0)

您的数组充满了results中的function calls,而不是您现在设置的functions中的数组。您想要存储的函数(匿名或非匿名)并在您的for循环中执行。

您还可以考虑创建一个函数,该函数执行每个函数并返回包含结果的数组。例如,您可以Array.map遍历数组,并且由于map也返回数组,因此您可以为每个函数获得实际结果。让我们调用函数invokeMap(类似于lodash具有的same exact function):

let add2 = x => x + 2

var fnArray = [  // array of anonymous functions
  (x) => x,  
  (x) => add2(x),
  (x) => add2(x) + add2(x)
];

let invokeMap = (arr, ...parrams) => arr.map((fn => fn(...parrams)))
 
let result = invokeMap(fnArray, 1) // pass one as a parameter to all functions

console.log(result)