SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2008
    Location
    Italy
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Create variable name from an array value

    Hi,

    Please can you help me?
    In javascript is it possible to create a var name from an array value?
    (I will assign to that name the return value of a function)

    I have this example, I want to create a var name from array_example[0][1]. Is it possible?:
    Code:
       
    array_example = new Array(new Array("value_0_0", "value_0_1"));
    
    for (r = 0; r <= array_example.length - 1; r++) {
    
    var array_example[r][1] = name_of_a_function();
    }
    executing this code I should obtain:
    Code:
    var value_0_1 = return_value_of_a_function

    many thanks.

  2. #2
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't think you can, but you can use the array value as the function name....
    Code:
    var value_0_1 = function() {
    		alert('called value_0_1');
    	},
    	array_example = [["value_0_0", "value_0_1"]],
    	r = 0;
    
    for (r; r <= array_example.length - 1; r++) {
    	var func = array_example[r][1];
    		window[func]();
    }


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •