JavaScript 数组对象的问题

Issue with javascript array object

本文关键字:问题 对象 数组 JavaScript      更新时间:2023-09-26

我有以下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 函数上变为真,并在 if 条件内发出警报,但没有进入 loadFromJson2 函数,尽管我传递了相同的参数。

谁能告诉我为什么 loadFromJson2 函数在我传递数组对象时没有收到内部警报?

如果input是一个数组,则不要调用loadFromJson2。 仅当input不是数组时才调用它。 所以_.isArray(input)永远不会在loadFromJson2里面是真的.

这里有一个 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嵌套调用 - 那些获取不是 Array 的内容的调用。