Seeking for a good method to create arrays out of the history array

Hi,

I currently have different methods implemented to create a certain history array. For example, to get a pure steps array, I do the following:

function createStepsArray(hist, curSteps) {
var arr = new [8];
arr = [0, 0, 0, 0, 0, 0, 0, 0];
arr[0] = curSteps;
if (null != hist && hist.size() > 0) {
for (var i = 0; i < hist.size(); ++i) {
if (null != hist&& null != hist.steps) {
arr[i+1] = hist.steps;
}
}
}
return arr;
}

[/CODE]

This works pretty well. Nevertheless, I have to duplicate the code for distance, for stairs, for stepgoal, etc. Is there a more intelligent way to create such an array? I would like to make "steps" a parameter of the function but I don't know if this is possible. Any suggestions?
  • Former Member
    Former Member
    Is "hist" a dictionnary ? If so hist["steps"] should work. Thus you could pass "steps" as a parameter ?

    If histis a class you could possibly pass a method as a parameter and the method would return the correct member ?
  • Thanks for that. I got it:

    function createHistArray(hist, current, label) {
    var arr = new [8];
    arr = [0, 0, 0, 0, 0, 0, 0, 0];
    arr[0] = current;
    if (null != hist && hist.size() > 0) {
    for (var i = 0; i < hist.size(); ++i) {
    if (null != hist&& null != hist[label]) {
    arr[i+1] = hist[label];
    }
    }
    }
    return arr;
    }
    [/CODE]

    And the call:
    stepsArr = createHistArray(hist, Amo.getInfo().steps, :steps);


    Thanks for pointing me in the right direction.