2013-07-30 93 views
0

我有一個從SQL Server數據庫獲取的實體框架5返回的以下JavaScript對象。如何將對象添加到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?

+4

'a.answers = [{}]','b.answers.push({})'? –

+0

「空答案對象」是什麼意思? – LoremIpsum

回答

1

喜歡的東西

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

可能是你在找什麼。

1

您可以在運行時在JavaScript中添加屬性或對象。例如

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

類似的,只是答案財產空白對象添加到您的一個&灣

a.answer = []; // Empty non null array 
b.answer = []; // " 
2
var answer=[]; 
a.push({ "answer":answer }); 
b.push({ "answer":answer });