i have two object one is for just to hold answer of one question and another is final object. like
answer = {"value":"asda","indicator":"good"};
final = {} ;
i want to append answer object to final object like this
final = {{"value":"asda","indicator":"good"},{"value":"sdad","indicator":"worse"}}
How can i do that ?
var answer = {"value":"asda","indicator":"good"};
var final = [] //declare it as Array.
final = final.concat(answer);
Updated answer :
With ecma6,its can done using spread operator
.
var answer = {"value":"asda","indicator":"good"};
final = {{"value":"sdad","indicator":"worse"}}
final = {...final,answer };//final as an object
OR
var answer = {"value":"asda","indicator":"good"};
final = {{"value":"sdad","indicator":"worse"}}
final = [...final,answer ]; //final as an array.
You cannot directly append an object. Instead, you need to hold object in an array like following.
answer = {"value":"asda","indicator":"good"};
final = [];
newObject = {"value":"sdad","indicator":"worse"};
now you can push both object to array like -
final.push(answer);
final.push(newObject);
Result of this will be -
final = [{"value":"asda","indicator":"good"},{"value":"sdad","indicator":"worse"}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With