SoulieBaby
SoulieBaby

Reputation: 5471

get array(s) from jquery / javascript function

I've got a javascript function which generates and returns a new array (of arrays):

function getFees(id){

    var prep = new Array, primary = new Array, secondary = new Array, vce = new Array;

    prep[0]         = 733;
    primary[0]      = 792;
    secondary[0]    = 879;
    vce[0]          = 1108;

    if (id == 2) {

        prep[1] = (prep[0] - prep[0] * 5 / 100);
        prep[1] = Math.ceil(prep[1]);

        primary[1] = (primary[0] - primary[0] * 5 / 100);
        primary[1] = Math.ceil(primary[1]);

        secondary[1] = (secondary[0] - secondary[0] * 5 / 100);
        secondary[1] = Math.floor(secondary[1]);

        vce[1] = (vce[0] - vce[0] * 5 / 100);
        vce[1] = Math.floor(vce[1]);

    } else if (id == 3) {

        prep[2] = (prep[0] - prep[0] * 10 / 100);
        prep[2] = Math.ceil(prep[2]);

        primary[2] = (primary[0] - primary[0] * 10 / 100);
        primary[2] = Math.ceil(primary[2]);

        secondary[2] = (secondary[0] - secondary[0] * 10 / 100);
        secondary[2] = Math.floor(secondary[2]);

        vce[2] = (vce[0] - vce[0] * 10 / 100);
        vce[2] = Math.floor(vce[2]);

    } else if (id == 4) {

        prep[3] = (prep[0] - prep[0] * 50 / 100);
        prep[3] = Math.ceil(prep[3]);

        primary[3] = (primary[0] - primary[0] * 50 / 100);
        primary[3] = Math.ceil(primary[3]);

        secondary[3] = (secondary[0] - secondary[0] * 50 / 100);
        secondary[3] = Math.ceil(secondary[3]);

        vce[3] = (vce[0] - vce[0] * 50 / 100);
        vce[3] = Math.floor(vce[3]);

    } else if (id >= 5) {

        prep[4] = (prep[0] - prep[0] * 75 / 100);
        prep[4] = Math.floor(prep[4]);

        primary[4] = (primary[0] - primary[0] * 75 / 100);
        primary[4] = Math.ceil(primary[4]);

        secondary[4] = (secondary[0] - secondary[0] * 75 / 100);
        secondary[4] = Math.ceil(secondary[4]);

        vce[4] = (vce[0] - vce[0] * 75 / 100);
        vce[4] = Math.floor(vce[4]);

    }

    var newArray = [];

    newArray.push({'prep':prep}); //prep array = 733,697
    newArray.push({'primary':primary}); //primary array = 792,753
    newArray.push({'secondary':secondary}); //secondary array = 879,835
    newArray.push({'vce':vce}); //vce array = 1108,1052

    return newArray;
}

Essentially I've given an example in the .push sections at the bottom. I then call my function by doing this:

var fees = getFees(2);
alert(fees);

Which alerts this:

[object Object],[object Object],[object Object],[object Object]

If I do:

alert(fees.toSource());

I get this:

[{prep:[733, 697]}, {primary:[792, 753]}, {secondary:[879, 835]}, {vce:[1108, 1052]}]

What I need to be able to do is get the number from any of the items (prep/primary/secondary/vce) eg..

fees.prep[0];
fees.primary[1];

But when I try that, I get this error:

TypeError: fees.prep is undefined

What am I missing?? Any help would be greatly appreciated!! :)

Upvotes: 0

Views: 36

Answers (1)

JYoThI
JYoThI

Reputation: 12085

you need to access like this

fees[0].prep[0];

Upvotes: 2

Related Questions