0

Newbie asking for help: I have an array of numbers that needs to be transformed to an array of functions that return the original numbers, so instead of calling a[3], I need to call call a[3]()

E.g.:

[1,2,3] -> [
    function () {return 1;}, 
    function () {return 2;}, 
    function () {return 3;}
]

How can I go about doing this?

5
  • Why in the world do you need to do this? If you explain the real problem you're trying to solve rather than this particular solution, then we'll probably be able to help you find a much better way to attack the problem. What you are asking for seems incredibly inefficient. Create a unique function for every value in an array is wasteful and I'm 100% sure there is a better way to solve your real problem if you can tell us what you're really trying to do. Commented May 16, 2012 at 1:29
  • The answers below all result in inefficient code - that was the point of my previous paragraph. It's simply dumb to create N function closures all to return values from an array. If you explained the real problem, you could probably create one function and no closures that would solve the problem much more efficiently. I was trying to help you find a BETTER way to solve your problem. If you don't care to learn a better or more efficient way to do things, then obviously I can't make you. Commented May 16, 2012 at 2:26
  • My question was my problem. I typed it word by word from a print out. It was just an interesting question that I couldn't work out hence why I'm here. Maybe its a trick question on its own. But thanks anyway! :) Commented May 16, 2012 at 2:31
  • If this is homework, you should add the homework tag to it. That would have also changed my response. Commented May 16, 2012 at 2:53
  • No its not homework, its a question I came across. Will keep the homework tag in mind for next time. Cheers buddy. Commented May 16, 2012 at 4:08

4 Answers 4

2
var arr = [1,2,3];

for (var i = 0; i < arr.length; i++)
    arr[i] = (function(i) { return function() { return i; } })(i);

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

Because before the edit, you didn't have the assignment there...? (It wasn't me who downvoted, just sayin').
Aaand I just realised that this doesn't actually answer the question :-p
1
[1, 2, 3].map(function(num) {
    return function(){ return num; }
});

should do the task

2 Comments

Yeah, but not in all browsers.
Or use a function that doesn't need it in the first place.
1
function convert( arr ) {
    for ( var i = 0; i < arr.length; i++ ) {
        arr[i] = (function( a ) { return function() { return a; }; })( arr[i] );
    }
    return arr;
}​

var arr = convert( [1, 2, 3] );

3 Comments

@Bergi—surely you're kidding. This is precisely the kind of thing that eval and Function as a function are meant for. Sure they have a little more overhead in creating the function, but they avoid the (useless, memory consuming) closure of the accepted answer.
Hey there, I'm a newbie trying to work this out, so I just picked the first answer that actually worked. Are you saying your answer is better? Can you please give me a quick explanation?
@muudless The accepted answer converts the array to functions that return the index (i.e. arr[0]() returns 0, not 1)... not the corresponding number from the previous array.
0
function transform(a) {
    var result = [];

    for (var i = 0; i < a.length; i++)
        result.push(function(value) {
            return function() {
                return value;
            }
        }(a[i]));

    return result;
}

var myArray = [1, 2, 3];
var transformed = transform(myArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.