0

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!! :)

3
  • 1
    try this fees[0].prep[0]; Commented May 30, 2017 at 2:34
  • 1
    if my answer is useful mark it with green tick it's useful for future user reference @Leanner Seawright Commented May 30, 2017 at 2:37
  • done just had to wait for it to let me :) Commented May 30, 2017 at 2:46

1 Answer 1

2

you need to access like this

fees[0].prep[0];
Sign up to request clarification or add additional context in comments.

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.