I am trying to find a way to use the JS Array.prototype.map()
functionality with a function that has another additional parameter (if possible at all, and I would like not to overwrite the built-in Array.prototype.map()
)., This documentation very good, but does not cover the case of "one or more additional parameters":
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
function doOpSingle(elem) { // do something with one array element } var A = ["one", "two", "three", "four"]; var x = A.map(doOpSingle); // this will execute doOpSingle() on each array element
So far so good. But what if the function in question has two parameters , for example e. a flag that you might want for it (think about bit masks)
function doOpSingle2(arrelem,flag) { // do something with one array element } var A = ["one", "two", "three", "four"]; var theFlag = util.getMask(); // call external function var y = A.map(doOpSingle2(theFlag)); // this does not work!
Any decisions should be made without for
loops, of course, because we have map()
to make our code cleaner and get rid of them!
source share