0

I'm trying to construct a multidimensional array in javascript, so far without success. I've read some other posts here on Stackoverflow, and read/watched tutorials on the subject, all have helped me but but I can't get it to work.

Most of the time I get the following error:

Uncaught SyntaxError: Unexpected token

Basically, I've got a form where the visitor of my website can input their age, bloodpressure, cholesterol and if they're a smoker of not.

riscTable = {      
'68': {    // AGE
    '170': {    // BLOODPRESSURE
                   // M, MS,  W, WS
        '275':     { 27, 48, 16, 30 },  // CHOLESTEROL
        '225':     { 22, 41, 13, 24 },  // CHOLESTEROL
        '175':     { 18, 34, 10, 20 },  // CHOLESTEROL
        '0':       { 15, 28, 8, 16 }    //  ...
    },
    '150': {
        '275':     { 20, 36, 11, 22 },
        '225':     { 16, 30, 9, 18 },
        '175':     { 13, 25, 7, 14 },
        '0':       { 10, 20, 6, 12 }
    },
    '130': {
        '275':     { 14, 27, 8, 15 },
        '225':     { 11, 22, 6, 13 },
        '175':     { 9, 18, 5, 10 },
        '0':       { 7, 15, 4, 8 }
    },
    '0': {
        '275':     { 10, 19, 5, 11 },
        '225':     { 8, 16, 4, 9 },
        '175':     { 6, 13, 3, 7 },
        '0':       { 5, 10, 3, 6 }
    }
},
'63': ...  // AGE
}

I've added comment tags to make my array structure more understandable.

What I was thinking on doing was to run over the key-value's with mathmatical logic (putting the heightest value always on top) to determine the result.

Example for the code above:

A non-smoking woman, age 65 (heigher as 63 so she falls under the 68 array), bloodpressure 150 and a cholesterol of 175 should return the value: 7

What am I doing wrong to get "Uncaught SyntaxError: Unexpected token" error, and how can I let my script run over the arrays to return the correct result, as shown in the example above?

Thank you for reading my question, I hope somebody can help me.

4

1 に答える 1

6

The portion of your code that contains the actual values is trying to use object notation.

'170': {    // BLOODPRESSURE
                   // M, MS,  W, WS
        '275':     { 27, 48, 16, 30 },  // Should be an array
        '225':     { 22, 41, 13, 24 },  // Should be an array
        '175':     { 18, 34, 10, 20 },  // Should be an array
        '0':       { 15, 28, 8, 16 }    //  ...
    },

Turn these into arrays.

var riscTable = {      
'68': {    // AGE
    '170': {    // BLOODPRESSURE
                   // M, MS,  W, WS
        '275':     [ 27, 48, 16, 30 ],  // Is an array
        '225':     [ 22, 41, 13, 24 ],  // Is an array
        '175':     [ 18, 34, 10, 20 ],  // Is an array
        '0':       [ 15, 28, 8, 16 ]    //  ...
    },//Rest of object
}};

You can then access the values as follows:

alert(riscTable['68']['170']['275'][0]);  //68 Year old male with bp of 170 and cholesterol of 275

Note that this is not a multidimensional array. It is an object that has properties with other objects assigned to them. Only the last properties, 275, 225,175,0 contain actual arrays.

I would hesitate to convert this complex object to an array since it would create the need to manage the indexes of each array and the subsequent nested array. Remember, multidimensional arrays are nothing more than arrays nested within arrays. If you take that approach you will need to know that the first index corresponds with the age range 68, as well as the index associated with the other age ranges as well as the nested properties such as cholesterol and blood pressure.

于 2013-03-03T22:40:39.173 に答える