1

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?

1
  • there looks to be extra commas, I think Commented Oct 20, 2013 at 18:02

1 Answer 1

1

You don't call loadFromJson2 if input is an array. You only call it if input is not an array. So _.isArray(input) will never be true inside loadFromJson2.

Here's a jsfiddle that demonstrates it (turn on your browser's javascript console to see the log). You get this output from your input:

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

As you can see, the calls to loadFromJson2 are from the nested calls to loadFromJson1 - the ones that get something that's not an Array.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi, i have added console.log("after return statement"); after return statement i can see that line which means coding is getting run after return statement. could you please tell me why are you saying that input is not an array it should be an array right?
If input is an array, then the body of that first if runs, including the return, which exits from loadFromJson1 completely. The only way to get past the return is to pass something to loadFromJson1 that is not an array. in which case, yes, it will turn around and call loadFromJson2, but since the thing it's passing to loadFromJson2 is not an array (it can't be, or it never would have gotten that far in loadFromJson1), it still doesn't get into the "if it's an array" block in loadFromJson2.
But here input is absolutely an array as you can the json response above. So the first if condition gets true always and complete the if condition and return the collection. Then it goes to next line here also i am passing the same parameter but it not getting true..i dont know why..of course in java we get an error if we do anything after return statement :)
Javascript never goes to the next line after a return. Ever. If you see the next line is executing, that means it didn't execute the return. So either you didn't pass an array, or that output is from a different call to the function. Most likely, the recursive call back to loadJson1 from before the return.
I added a link to a fiddle so you can see what's going on. You're being confused by the fact that loadFromJson1 calls itself when it gets an array, and those recursive calls are the ones that are getting to your log statement - because they aren't receiving arrays.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.