我有一个从实体框架5返回来自SQL Server数据库的以下javascript对象。
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?
答案 0 :(得分:2)
var answer=[];
a.push( { "answer":answer } );
b.push( { "answer":answer } );
答案 1 :(得分:1)
像
这样的东西var newAnswer = {"answerId":0,"correct":false,"response":false,"text":"","image":null,"questionId":0};
b.answers.push(newAnswer);
可能就是你要找的东西。
答案 2 :(得分:1)
您可以在JavaScript中在运行时添加对象的属性。例如
var a = { f : 10, g : 20 };
a.h = 30;
同样,只需将答案属性添加到您的a& b带有空白物体。
a.answer = []; // Empty non null array
b.answer = []; // "