2013-10-20 111 views
1

我有下面的JSON响应。我使用$ .getJSON方法来加载JSON数据,并使用回调函数通过检查它是否为数组来执行一些操作。javascript数组对象问题

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

上面的代码我传递json响应为loadFromJson1和loadFromJson2函数作为“输入”。 isArray只在loadFromJson1函数中获得true,并在if条件中给出警报,但是不在loadFromJson2函数中,尽管我传递的是相同的参数。

任何人都可以告诉我为什么loadFromJson2函数没有得到内部警报如果条件虽然我传递数组对象?

+0

看起来有额外的逗号,我认为 – ajax333221

回答

1

如果input是数组,则不调用loadFromJson2。只有在input而不是的数组时才会调用它。因此在loadFromJson2_.isArray(input)将永远不会成立。

Here's a 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 - 那得到的东西,是不是一个阵的人。

+0

嗨,我已经添加了console.log(“之后返回语句”);在return语句之后,我可以看到那个代码在return语句后运行。你能告诉我为什么你说输入不是数组,它应该是一个数组吗? – ezhil

+0

如果'input'是一个数组,那么第一个'if'的主体将运行,包括'return',它将从'loadFromJson1'完全退出。通过'return'的唯一方法是将一些东西传递给不是数组的'loadFromJson1'。在这种情况下,是的,它会转过来调用'loadFromJson2',但是因为它传递给'loadFromJson2'的东西不是一个数组(它不能,或者它从来不会在'loadFromJson1'中得到那么多) ,它仍然不会进入'loadFromJson2'中的“如果它是一个数组”块。 –

+0

但是这里的输入绝对是一个数组,因为你可以使用上面的json响应。所以第一个if条件总是成立并完成if条件并返回集合。然后它转到下一行这里我也传递相同的参数,但它并没有得到真正的..我不知道为什么..当然在Java中我们得到一个错误,如果我们做任何事后返回语句:) – ezhil