0

我有以下从 Entity Framework 5 返回的 javascript 对象从 SQL Server 数据库获取。

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}]
 }

我想添加一个空的答案对象,然后使用 PUT 发送回服务器。

如何将答案对象添加到变量 a 和变量 b ?

4

3 回答 3

2
var answer=[];
a.push( { "answer":answer } );
b.push( { "answer":answer } );
于 2013-07-30T06:26:38.337 回答
1

就像是

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

可能是您正在寻找的。

于 2013-07-30T06:25:24.830 回答
1

您可以在 JavaScript 中在运行时添加对象的属性。例如

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

同样,只需将 answers 属性添加到您的 a & b 与空白对象。

a.answer = []; // Empty non null array
b.answer = []; // "
于 2013-07-30T06:26:02.990 回答