仅显示一个输出

时间:2019-06-30 16:12:01

标签: javascript html ajax csv dictionary

执行for循环后,此函数返回单个输出(explanation),而不是多个输出(在每次迭代中,该函数必须返回不同的explanation值)。

data是一个数组,其中包含我应该在for循环中进行的迭代次数的信息。

例如:如果data = ['city', 'b', 'c'],该函数仅返回与字符串'c'对应的结果,而不对与字符串'b'对应的文件进行寻址(出于这个原因,我不需要'city'周期从i = 1开始)。

我应该如何修改setExplanation函数,以便在每次迭代时验证explanation变量?

function setExplanation1(dati, url)
{
    var element = document.getElementById('explanation1');

    num_totali = dati.length - 1;
    num_spiegati = 0;

    while (element.firstChild) 
    {
        element.removeChild(element.firstChild);
    }

    var para = document.createElement("p");
    var node = document.createTextNode('Ti suggerisco di provare '+ nome_locale + '.');

    para.appendChild(node);
    element.appendChild(para);

    console.log(dati);

    for(var i=0;i < dati.length;i++) 
    {
        var mapReview1 = new Map();
        var contesto = dati[i];

        // read text from URL location
        var urlExplanation = "";
        var request = new XMLHttpRequest();
        var path = "js/"+conf_selected+"/"+contesto+"_1phrase.csv";

        request.open('GET', path);
        request.send();

        request.onreadystatechange = function() 
        {
            console.log(i);

            if (request.readyState === 4 && request.status === 200)  {
                var type = request.getResponseHeader('Content-Type');
                urlExplanation = request.responseText;
            }

            var line = urlExplanation.toString().split('\n');

            for (var j = 1; j < line.length; j++) 
            {
                let url = line[j].split(';')[0];
                let frase = line[j].split(';')[1];

                mapReview1.set(url,frase);
            }

            for (var key of mapReview1.keys()) 
            {
                if(key === url) {
                    let explanation1 = mapReview1.get(key);
                    console.log(explanation1);
                    num_spiegati = num_spiegati+1;
                    console.log(num_spiegati);

                    let para1 = document.createElement("p");
                    let node1 = document.createTextNode('E’ un locale adatto per ' + getStringContext(contesto) + ' per questo motivo: "'+ explanation1 +'"');

                    para1.appendChild(node1);
                    element.appendChild(para1);
                }
            }
        }
    }

    console.log('totali : '+ num_totali);
    console.log('spiegati : '+ num_spiegati);
}

0 个答案:

没有答案