Issue with javascript array object

Posted by ezhil on Stack Overflow See other posts from Stack Overflow or by ezhil
Published on 2013-10-20T15:51:38Z Indexed on 2013/10/20 15:53 UTC
Read the original article Hit count: 357

Filed under:
|
|
|

I have the below JSON response. I am using $.getJSON method to loads JSON data and using callback function to do some manipulation by checking whether it is array as below.

{
    "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,
        }],
    }]
}

I am passing the json responses on both loadFromJson1 and loadFromJson2 function as "input" as parameter as below.

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;
        }
    };

The above code i am passing json responses for both loadFromJson1 and loadFromJson2 function as "input". isArray is getting true on only loadFromJson1 function and giving alert inside the if condition but not coming in loadFromJson2 function though i am passing the same parameter.

can anyone tell me why loadFromJson2 function is not getting the alert inside if condition though i pass array object?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery