Suppose there are arrays arr1 and arr2 . The first array contains numbers. It is necessary to rewrite it into the second array so that it contains functions. When calling arr2[3]() we get the same result as when calling arr1[3] . I implemented it like this:

 var arrFunc = []; function convertArr(arr) { for (var i = 0; i < (arr.length - 1); i++) { arrFunc.push(function(x) { return arr[x] }(i)); } } 

But in this case it does not work correctly. You need to call arrFunc[3](3) .

  • So you need to call arr2[3]() (a parameter is not passed to the function) or arrFunc[3](3) (a parameter is passed to the function, plus we assume that arr2 and arrFunc are the same thing)? - Regent
  • In my case, it is incorrect, what is called with the parameter, but according to the condition it is necessary without. yes, arr2 arrFunc is the same thing - V. Rotenberh

1 answer 1

I would use the map function:

 arr.map(function(element) { return function() { return element; }; });