Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

If I have something like this:

var quiz = [{
    "questionID": "KC2Q4",
    "correctAnswer": "KC2Q4a"
},{
    "questionID": "KC2Q5",
    "correctAnswer": "KC2Q5b" 
}];

and have a variable that we can call "question" that has a value of a string like KC2Q4. How do I return the "correctAnswer" for the "questionID" that matches the variable "question" in a new variable "answer"?

share|improve this question

marked as duplicate by Felix Kling javascript Jan 6 '15 at 22:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
This SO question is what you are looking for. – The_Black_Smurf Jan 6 '15 at 22:07
    
Yes, this is exactly what I was looking for! Thank you. That post didn't come up in my search before I posted. :/ – Jonathan Bruch Jan 6 '15 at 22:24

You essentially want to iterate over your array, checking each object for the proper questionID. When you find that object, return the correctAnswer property of that object.

var question = "KC2Q4";
for( var i=0; i<quiz.length; i++ ){
  if( quiz[i].questionID === question ){
    return quiz[i].correctAnswer;
  }
}
share|improve this answer

You should use Array.prototype.filter function (note filter() is a ECMA-Script 5.x native function: you don't need third-party libraries or frameworks!!):

var correctAnswer = "KC2Q4a";

// "filter" is like a "where". It iterates each object in your array
// and returns ones that fit the given condition as a closure:
var answersFound = quiz.filter(function(question) {
    return question.correctAnswer == correctAnswer;
});

// You could verify if length > 0, but you want to be sure that
// there's only a single match for a given correct answer, because I feel
// that "correctAnswer" is like an unique id...
if(answersFound.length == 1) {
   // Since there's a found answer to given "correctAnswer",
   // you get the single result (i.e. the question object):
   var answer = answersFound[0];
}

If you find above checking useless (in my case, I would call it defensive programming), you may retrieve the question object directly this way:

// Without the checking done in the other code listing, you've two risks:
// a. "quiz" could contain no question objects and the filter will return zero results
//    meaning that getting first result array index will throw an error!
//
// b. "quiz" could contain question objects but what you're looking for isn't 
//    present in the so-called array. Error too!
var answer = quiz.filter(function(question) {
    return question.correctAnswer == correctAnswer;
})[0];
share|improve this answer
    
@zerkms I was going to do this way. I've updated my answer with your suggestion. – Matías Fidemraizer Jan 6 '15 at 22:14
    
@zerkms BTW, I've re-updated with pros/cons about your suggestion :D – Matías Fidemraizer Jan 6 '15 at 22:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.