English 中文(简体)
JavaScript 函数不如预期返回数组
原标题:JavaScript function not returning array as expected

我有一个 json 阵列和一个简单函数可以返回一些数据。 我可以登录我要输入到控制台的数据( 函数目前用于测试), 但是它不会返回 。

斯塔克斯,请帮助我 破旧的大脑 并告诉我,我在哪里 已经疯了。

(代码是超级自我解释,函数呼叫位于.js底部)

http://jsfiddle.net/BDJeU/

function getCountryData(data, country)
{
    $.each(data, function(index) {

        if( data[index]["Country"] == country )
        {
            console.log( data[index]["Country"] );
            console.log( data[index]["data"] );
            return data[index]["data"];
        }
    });
}
最佳回答

尝试此 :

http://jsfiddle.net/BDJeU/3/

    function getCountryData(data, country)
    {
        var returnData;
        $.each(data, function(index) {

            if( data[index]["Country"] == country )
            {
                returnData = data[index]["data"];
                return false;
            }
        });
        return returnData;
    }

之所以不起作用,是因为您正在返回到 each 函数。所以设置一个变量,在迭代之外分配值,可以获取您想要的数据。

问题回答

您的返回位于 ache 所使用的匿名函数中。 因此, getcountryData 本身没有返回( 因此默认返回未定义 ) 。 它需要这样的东西 :

function getCountryData(data, country)
{
    var result;
    $.each(data, function(index) {

        if( data[index]["Country"] == country )
        {
            console.log( data[index]["Country"] );
            console.log( data[index]["data"] );
            result = data[index]["data"];
        }
    });

    return result;
}

$.grep () 在这种情况下可能更好: < a href="http://jsfidle.net/zerkms/BDJeU/12" rel="nofollow" >http://jsfiddle.net/zerkms/BDJeU/12/

function getCountryData(data, country)
{
    var result = $.grep(data, function(item) {
        return item["Country"] == country;
    });

    return result && result[0] && result[0][ data ];
}

您需要返回 ach 外的 ach , 使用 return 来打破 ach 迭代的早期 :

function getCountryData(data, country)
    {
        var res;
        $.each(data, function(index) {

            if( data[index]["Country"] == country )
            {
                res = data[index]["data"];

                // once found, stop searching and
                // break early out of the each iteration                  
                return; 
            }
        });
        return res;
    }

您真的应该使用每一个吗? 如果您试图将结果过滤到一个单一的回答尝试

function getCountryData(data, country) {
    var matchingCountries = $.grep(data, function(row){
        return row.Country == country;
    });
    if (matchingCountries.length > 0)
        return matchingCountries[0].data;
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.