0

I have the following javascript object that came back from an Entity Framework 5 get from a SQL Server database.

var a =
 {
  "questionId":14,
  "questionStatusId":1,
  "answers":null
 }

var b =
 {
  "questionId":15,
  "questionStatusId":1,
  "answers":[
   {"answerId":34,"correct":true,"response":false,"text":"5","image":null,"questionId":15},
   {"answerId":35,"correct":false,"response":false,"text":"50","image":null,"questionId":15}]
 }

I would like to add an empty answer object and then send back to the server with a PUT.

How can I add an answer object to variable a and to variable b ?

2
  • 4
    a.answers = [{}], b.answers.push({})? Commented Jul 30, 2013 at 6:23
  • What do you mean by "an empty answer object" ? Commented Jul 30, 2013 at 6:24

3 Answers 3

2
var answer=[];
a.push( { "answer":answer } );
b.push( { "answer":answer } );
Sign up to request clarification or add additional context in comments.

Comments

1

Something like

var newAnswer = {"answerId":0,"correct":false,"response":false,"text":"","image":null,"questionId":0};
b.answers.push(newAnswer);

is probably what you are looking for.

Comments

1

You can add properties o objects at runtime in JavaScript. For example

var a = { f : 10, g : 20 };
a.h = 30; 

Similarly, just add the answers property to your a & b with blank objects.

a.answer = []; // Empty non null array
b.answer = []; // "

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.