javascript change function parameter variable name and return its value -



javascript change function parameter variable name and return its value -

hi have function , want homecoming function parameter added value parameter. instead of writing this:

function(response) { homecoming response.links_1; } function(response) { homecoming response.links_2; } function(response) { homecoming response.links_3; }

i want create loop iterates through , adds number, this:

function(response) { var counter = 3; for(var = 0; < counter; i++) { homecoming response.links_ +i; } }

the of import part response.link_ must not string! looses function parameter value.

i tried doing this:

function(response) { var = 1, resp = 'response.links_', endresp = resp + i; homecoming endresp ; } }

and console.log(endresp); returns right string, thats it, string.. want value of variable response.links_1 not string value response.links_1.

i have tried next without luck: (the parse: backbone method)

parse: function(response) { var counter = 3; for(var = 0; < counter; i++) { homecoming response[links_ + i]; } }

any help welcome..

to access keys of json hash, there 2 ways

// next object var obj = { links_1: 'value1', links_2: 'value1', links_3: 'value1', }; // key1 can accessed console.log(obj.links_1); // or console.log(obj['links_1']);

so, in case, can utilize sec method

var counter = 3; for(var = 0; < counter; i++) { homecoming response['links_' + i]; }

also, can total count by

var counter = object.keys(response).length;

and, if don't need else counter variable, , response object contains links, can utilize like

for (link in response) { homecoming response[link]; }

javascript function variables parameters

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -