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!

3 Answers 3

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]);
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer and step by step !
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".

1 Comment

What if i took the parameter in and assigned it to a global then used it would this work
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']

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.