我有以下JSON响应。我正在使用$ .getJSON方法加载JSON数据,并使用回调函数通过检查其是否为数组来进行一些操作,如下所示。

{
    "r": [{
        "IsDefault": false,
        "re": {
            "Name": "Depo"
        },
        "Valid": "Oct8, 2013",
        "Clg": [{
            "Name": "james",
            "Rate": 0.05
        }, {
            "Name": "Jack",
            "Rate": 0.55
       }, {
            "Name": "Mcd",
            "Rate": 0.01,
        }],
    },
    {
        "IsDefault": false,
        "re": {
            "Name": "Depo"
        },
        "Valid": "Oct8, 2013",
        "Clg": [{
            "Name": "james",
            "Rate": 0.05
        }, {
            "Name": "Jack",
            "Rate": 0.55
       }, {
            "Name": "Mcd",
            "Rate": 0.01,
        }],
    },
    {
        "IsDefault": false,
        "re": {
            "Name": "Depo"
        },
        "Valid": "Oct8, 2013",
        "Clg": [{
            "Name": "james",
            "Rate": 0.05
        }, {
            "Name": "Jack",
            "Rate": 0.55
       }, {
            "Name": "Mcd",
            "Rate": 0.01,
        }],
    }]
}


我正在通过loadFromJson1和loadFromJson2函数的json响应作为“输入”作为参数,如下所示。

var tablesResult = loadFromJson1(resultstest.r[0].Clg);
    loadFromJson1 = function (input) {
        if (_.isArray(input)) {
        alert("loadFromJson1: Inside array function");
            var collection = new CompeCollection();
            _.each(input, function (modelData) {
                collection.add(loadFromJson1(modelData));
            });
            return collection;
        }
        return new CompeModel({
            compeRates: loadFromJson2(input),
            compName: input.Name
        });
    };

    loadFromJson2 = function (input)
    // here is the problem, the 'input' is not an array object so it is not going to IF condition of the isArray method.
    {
        if (_.isArray(input)) {
            alert("loadFromJson2: Inside array function");
            //alert is not coming here though it is an array
            var rcollect = new rateCollection();
            _.each(input, function (modelData) {
                rcollect.add(modelData);
            });
            return rcollect;
        }
    };


上面的代码我将loadFromJson1和loadFromJson2函数的json响应作为“输入”传递。 isArray仅在loadFromJson1函数上为true,并且在if条件内给出警报,但是虽然我传递了相同的参数,但没有在loadFromJson2函数中使用。

谁能告诉我为什么我通过数组对象的情况下,loadFromJson2函数为什么没有在内部发出警报?

最佳答案

如果loadFromJson2是数组,则不调用input。仅当input不是数组时才调用它。因此_.isArray(input)loadFromJson2内永远不会为真。

Here's a jsfiddle进行演示(打开浏览器的JavaScript控制台以查看日志)。您可以从输入中得到以下输出:

into loadFromJson1 call #1 (index):82
loadFromJson1 #1: it's an Array (index):84
loadFromJson1 #1: Inside array function (index):85
into loadFromJson1 call #2 (index):82
loadFromJson1 #2: not an array (index):93
loadFromJson1 #2: calling loadFromJson2 and returning (index):95
into loadFromJson2 (index):105
loadFromJson2: not an array (index):115
into loadFromJson1 call #3 (index):82
loadFromJson1 #3: not an array (index):93
loadFromJson1 #3: calling loadFromJson2 and returning (index):95
into loadFromJson2 (index):105
loadFromJson2: not an array (index):115
into loadFromJson1 call #4 (index):82
loadFromJson1 #4: not an array (index):93
loadFromJson1 #4: calling loadFromJson2 and returning (index):95
into loadFromJson2 (index):105
loadFromJson2: not an array (index):115
loadFromJson1 #1: returning (index):90


如您所见,对loadFromJson2的调用来自对loadFromJson1的嵌套调用-那些获得的不是数组的调用。

10-06 15:58