1

以下の 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 関数が内部でアラートを取得しない理由を誰か教えてもらえますか?

4

1 に答える 1

1

配列のloadFromJson2場合は呼び出しません。が配列でないinput場合にのみ呼び出します。内部では決して true にはなりません。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ものを取得するものです。

于 2013-10-20T15:55:06.330 に答える