2013-06-24 36 views
0

我有一個名爲questionSets的對象數組。 createSet函數應該創建新的或創建一個現有的questionSets對象的副本。如果createSet用於複製,則使用getQuestionsFromSet函數。出於某種原因,當我從createSet()內部調用getQuestionsFromSet()時,我總是得到一個返回值'undefined'。我無法弄清楚爲什麼,因爲當我做一個getQuestionsFromSet()返回的值的console.log()時,我確切地看到了我想要的。爲什麼我的返回值未定義(JavaScript)

我有這兩個功能。

​​
+0

console.log顯示值? –

+0

@AnkitJaiswal那裏沒有Ajax電話! – epascarello

回答

5

因爲getQuestionsFromSet()return什麼,所以隱含不確定

你需要的大概是這樣的:

function getQuestionsFromSet(setName) { 
    var matched = []; // the array to store matched questions.. 
    $.each(questionSets, function (index, obj) { 
     if (obj.label == setName) { 
      console.log(obj.value); // array with some objects as values, which is what I expect. 
      matched.push(obj.value); // push the matched ones 
     } 
    }); 
    return matched; // **return** it 
} 
2

return obj.value;嵌套在內$.each(function{})內,並getQuestionsFromSet確實不返回任何東西。

相關問題