0

Jquery Each Json Values Issue

This question is similar to above, but not the same before it gets marked duplicated.

After realasing how to use computed values i came across another issue.

In my javascript i have the following code:

var incidentWizard = ['page1.html','page2.html','page3.html'];
var magicWizard = ['page1.html','page2.html','page3.html'];
var loadedURL = 'page1.html';

The input to this function would be (true,'incident')

function(next,wizardname)
 {

   var WizSize = incidentWizard.length; 
    wizardName = [wizardName] + 'Wizard';

   var wizardPOS = jQuery.inArray(loadedURL,incidentWizard);

And now i want to use the wizardname parameter to decide what array i am going to use...

Loader(incidentWizard[wizardPOS],true);

Ive also tried

Loader([incidentWizard][wizardPOS],true);

and

Loader([incidentWizard][wizardPOS],true);

Also the loader function just required the string value in the array at wizardPOS sorry for confusion

But when trying this i always end up with the outcome...

/incidentWizard  

I know this is something to do with using computed values but i've tried reading about them and cant seem to solve this issue.

Basicly i want to use the computed value of wizardName to access an an array of that name.

Please help supports, looking forward to seeing many ways to do this!

Community
  • 1
  • 1
Lemex
  • 3,772
  • 14
  • 53
  • 87

3 Answers3

1

You can only access properties of objects that way. For global values, window[ name ] will work. For simple local variables it's just not possible at all. That is, if inside a function you've got

var something;

then there's no way to get at that variable if all you have is the string "something".

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • What if i took the parameter in and assigned it to a global then used it would this work – Lemex Jun 22 '12 at 15:09
1

I would just put each array as a prop on an object:

var obj {
    incidentWizard: ['page1.html','page2.html','page3.html'],
    magicWizard: ['page1.html','page2.html','page3.html']
};

Then you can just do obj['incidentWizard'] or obj.incidentWizard this will return:

['page1.html','page2.html','page3.html']
Paul
  • 12,392
  • 4
  • 48
  • 58
1

On this line:

wizardName = [wizardName] + 'Wizard';

You are attempting to concatenate the string 'Wizard' to an Array with one string element "incident". I'm assuming you just want regular string concatenation:

wizardName = wizardName + 'Wizard';

However, now you only have a string, not an array instance. To fix that, change the way you define your *Wizard arrays to something like:

var wizardyThings = {
    incidentWizard : ['page1.html','page2.html','page3.html'],
    magicWizard: ['page1.html','page2.html','page3.html']
};

Then your function (which is missing a name as it stands), becomes:

function someMethod(next, wizardname) {

    wizardName = wizardName + 'Wizard';
    var wizSize = wizardyThings[wizardName].length;

    var wizardPOS = jQuery.inArray(loadedURL, wizardyThings[wizardName]);
    ...
}
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194