0

I am just skeezing my brain without any result so far.

What I am trying to do is execute/call a function (ParamLecture) within another one (syncronicFeatures).

ParamLecture just reads some colums from a document given an argument (i.e wind; this document contains several oceanographic parameters) and by itself it works properly; returning the targeted array.

function ParamLecture(Param) {
    var col;
    if (Param === "wind") {
        col = [21, 22]; //(modulo y dir) m/s
    } else if (Param === "current") {
        col = [16, 17]; //(modulo y dir) cm/s
    } else if (Param === "times") {
        col = [0, 1, 2, 3]; //(AA-MM-DD-HH)
    }
    console.log(col)
    var ArrayParam = [];
    var result1;
    var result;
    d3.tsv("/prueba/REDEXT_T_HIS_GolfoDeCadiz_Wind.tsv", function(data) {
        for (var i = 0; i < data.length; i++) {
            result1 = (data[i]["AA MM DD HH    Hm0   Tm02    Tp   Hmax  Thmax  Pro_Oe   Dmd   Dmd_P  Ds_P  Pro_Od   Ts2    Sa2   Vc_md  Dc_md Pro_Oce   Ps     Ta  Vv_md  Dv_md   Pro_Met Qc_e"]);
            result = result1.split(" ").filter(function(obj) {
                return obj != ""
            });
            if (col.length > 2) {
                ArrayParam.push(result[col[0]] + '-' + result[col[1]] + '-' + result[col[2]] + '-' + result[col[3]]);
            } else {
                ArrayParam.push([result[col[0]], result[col[1]]])
            }
        }
    });

    return ArrayParam;
}

my call:

var t = ParamLecture("wind") (where t contains the desired array)

The problem arises when I try to do the same within the 'syncronicFeatures' calling ParamLecture and preserving its output inside a variable (always an empty array when I check). Let me show you the code so that maybe you can just find where my common sense went to waste:

function syncronicFeatures(date, time, Param1, Param2) {

    var hours = [];
    var hour;
    for (var j = 0; j < time.length; j++) {
        hours.push(time[j][0].split(":")[0]);
    } //array hours
    //console.log(hours) --> right output

    hour = hours.filter(function(item, position) {
        return hours.indexOf(item) == position;
    }) //array unique hours
    //console.log(hour) --> right output

    var equivalent = [];
    for (var i = 0; i < hour.length; i++) {
        equivalent.push(date + '-' + hour[i])
    } //array notation: AA-MM-DD-HH
    //console.log(equivalent) --> right output

    var hourlydate = ParamLecture(Param1);
    //console.log(hourlydate) //empty array????? --> WRONG output
    var arrayParam = ParamLecture(Param2);
    //console.log(arrayParam) //empty array??? --> WRONG output

    var CurrParam = []; //array Param syncronic
    for (var k = 0; k < equivalent.length; k++) {
        var pos = hourlydate.indexOf(equivalent[k])
        CurrParam.push(arrayParam[pos])
    }

    return CurrParam //array Param syncronic
}

my call:

var sync = syncronicFeatures(date, time, "times", "wind")

Console does not show any error; just gives me sync as an array of undefined elements because as hourlydate and arrayParam are empty arrays, the for that comes after does not find elements (indexOf=-1; which in turn, as position, does not exists).

Thank you for every input in advance; I am eager to know your thoughts! I owe you one!

Will
  • 2,163
  • 1
  • 22
  • 22
  • 1
    *anonimus = anonymous --- also, please make sure your code is formatted well, otherwise it can be hard to read. – evolutionxbox Jul 19 '16 at 15:15
  • `d3.tsv` does its work *asynchronously*, you can't *return* the result of that work. You have to use a callback mechanism (directly, or via promises). See the linked question's answers for how you do that. – T.J. Crowder Jul 19 '16 at 15:20
  • See the linked question and its answers. Your `ParamLecture` returns **before** any entries have been put in the array by the `d3.tsv` callback. It's asynchronous. It only *starts* when you call `d2.tsv`, the work doesn't *finish* until later, which is why `d3.tsv` calls a callback rather than returning the data to you. – T.J. Crowder Jul 19 '16 at 15:22
  • Thank you very much T.J. Crowder, I will check what you have pointed out! thanks a lot! – Paz Rotllán García Jul 19 '16 at 15:24

0 Answers0