0

Here is the one which i am trying to achieve.

I have the below JSON response. I am using $.getJSON method to loads JSON data and adding it in the collection 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 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. Hence i am trying to convert into array object.
    {
        if (_.isArray(input)) {
            alert("loadFromJson2: Inside array function");
            //alert is not coming here though it is array
            var rcollect = new rateCollection();
            _.each(input, function (modelData) {
                rcollect.add(modelData);
            });
            return rcollect;
        }
    };

The above code i am passing json responeses 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 you please tell me what mistakes i am doing here?

ezhil
  • 977
  • 6
  • 15
  • 36
  • 2
    Your JSON response already is an array! It's an array of objects with the properties `Name` and `Rate`. Please give an example of the result you are expecting. – Chris Pickford Oct 18 '13 at 09:37
  • possible duplicate of [How to convert JSON object to JavaScript array](http://stackoverflow.com/questions/14528385/how-to-convert-json-object-to-javascript-array) – Hariharan Oct 18 '13 at 09:38
  • In your function, you are only returning values, so what you are getting is what you should be expecting. – spacebean Oct 18 '13 at 09:39
  • @Chris, do you understand now why i am trying to convert into array object? while passing parameter to the function it is not going as array. – ezhil Oct 18 '13 at 10:51
  • I think you have convoluted this further. Please clarify exactly what you are trying to achieve. – Chris Pickford Oct 18 '13 at 10:56
  • Though i am passing same parameter on loadFromJson2 function, isArray method is getting false. i have updated my post, can you please check it once? – ezhil Oct 18 '13 at 12:26

1 Answers1

0

Your code is a bit scattered and the function loadFromJson1 is duplicted. Also some objects are not posted in de question.

I did however created a fiddle which work; "Inside array function" is printed...

simpliefied version of loadFromJson1 which is working in the fiddle:

loadFromJson1 = function (input) {
    var resArray = $.map(input, function (value, index) {
        return [value];
    });
    if (_.isArray(resArray)) {
        console.log("Inside array function");
    }
};
R. Oosterholt
  • 7,720
  • 2
  • 53
  • 77