Reputation: 222
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 ?
Upvotes: 6
Views: 23329
Reputation: 15292
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.
Upvotes: 3
Reputation: 610
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"}];
Upvotes: 1
Reputation: 644
If your using ES2015 you could do
const answer = {"value":"asda","indicator":"good"};
const final = [].concat(Object.assign({}, answer))
Or Ghazanfar' is also valid
Upvotes: 1
Reputation: 3718
In order to append answer object into final , final must be an array
var final =[];
final.push({"value":"asda","indicator":"good"})
or
final.push(answer);
Upvotes: 1