Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append object into existing object Angularjs

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 ?

like image 508
Suraz Khanal Avatar asked Nov 10 '16 08:11

Suraz Khanal


2 Answers

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.
like image 111
RIYAJ KHAN Avatar answered Sep 30 '22 05:09

RIYAJ KHAN


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"}];
like image 45
Avinash Rathod Avatar answered Sep 30 '22 06:09

Avinash Rathod