2013-06-24 3 views
0

개체가 가득 찬 questionSets 배열이 있습니다. createSet 함수는 new를 만들거나 기존 questionSets 객체의 복사본을 만들어야합니다. getQuestionsFromSet 함수는 createSet을 사용하여 복사를하는 경우에 사용됩니다. 어떤 이유로 나는 createSet() 내부에서 getQuestionsFromSet()을 호출 할 때 항상 반환되는 'undefined'값을 얻습니다. 나는 왜 내가 원하는 것을 정확히 볼 수 있기 때문에, getQuestionsFromSet()에 의해 리턴 될 값의 console.log()를 할 것이기 때문에 이유를 알 수 없다.반환 값이 정의되지 않은 이유 (JavaScript)

나는이 두 가지 기능을 가지고 있습니다.

function createSet(name, copiedName) { 
    var questions = []; 
    if (copiedName) { 
     questions = getQuestionsFromSet(copiedName); 
    } 
    console.log(questions); // undefined. WHY?? 
    questionSets.push({ 
     label: name, 
     value: questions 
    }); 
}; // end createSet() 

function getQuestionsFromSet(setName) { 
    $.each(questionSets, function (index, obj) { 
     if (obj.label == setName) { 
      console.log(obj.value); // array with some objects as values, which is what I expect. 
      return obj.value; 
     } 
    }); 
}; // end getQuestionsFromSet() 
+0

console.log에 값이 있습니까? –

+0

@AnkitJaiswal 아약스 호출이 없습니다! – epascarello

답변

5

getQuestionsFromSet()하지 return 아무것도하지 그래서 암시 정의되지 않은 때문입니다. 당신이 필요로하는 무엇

아마 같은 것입니다 : 내부 $.each(function{}) 내에 중첩되어

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;getQuestionsFromSet 아무것도 반환하지 사실이다.

관련 문제