0

I'm sending data from my client like this:

data = {
    Checked: ??
    Matches: null,
    Text: null,              
}

My data is stored like this:

var a = {
     "Answers": [
         {
             "answerUId":1,
             "correct":true,
             "checked":true,
             "text":"`public`"
         },
         {
             "answerUId":2,
             "correct":false,
             "checked":true,
             "text":"`static`"
         }
   ]
}

How can I get out the values of just the checked field and use it to make another array but with just the one parameter so I can use this to replace the ?? in my code?

What I need is:

[ true,
  false ]
3
  • means you just want those Answers which has checked:true? Commented Jul 21, 2014 at 7:13
  • What exactly do you want? An array with just the value of checked from ever set of data? Commented Jul 21, 2014 at 7:14
  • I updated the question to show in more detail what I am looking for. I think just a simple array where each row has one value of true or false. Commented Jul 21, 2014 at 7:28

1 Answer 1

1

If I understood your problem correctly, This should work for you

var a = 
{"Answers":
   [
    {"answerUId":1,
     "correct":true,
     "checked":true,
     "text":"`public`"},
    {"answerUId":2,
     "correct":false,
     "checked":true,
     "text":"`static`"},
     {"answerUId":3,
     "correct":false,
     "checked":false,
     "text":"`static`"}
   ]
}

var checkedAnswers = a.Answers.map(function (answer) {
    return answer.checked;
})

console.log(checkedAnswers);

CheckedAnswers will be an array like bellow

[ true, true, false ]
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry maybe I did not explain well. I just added to the questin to show what I think is needed.
@Alan no Probs , I just updates my answer, just change filter to map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.