javascript数组对象的问题

时间:2013-10-20 15:51:39

标签: javascript jquery json underscore.js

我有以下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响应作为参数传递给“input”,如下所示。

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响应作为“input”传递。 isArray仅在loadFromJson1函数上变为true,并在if条件内发出警告但在loadFromJson2函数中没有,但我传递相同的参数。

任何人都可以告诉我为什么loadFromJson2函数没有得到内部警报虽然条件虽然我传递数组对象?

1 个答案:

答案 0 :(得分:1)

如果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嵌套调用 - 那些得到的内容的调用阵列。