javascript - How to add a property to an object -


i need pass exact data webapi:

{  email: "useremail",  password: "userpassword",  grant_type: "password" } 

from login form, receive following data:

{  email: "useremail",  password: "userpassword" } 

i need add grant_type: "password" object.

here method

signin(credentials) {    console.log(credentials);    this.authservice.login(credentials)      .subscribe(result => {        if (result)         this.router.navigate(['/']);       else           this.invalidlogin = true;      }); } 

the credentials data coming login form.

if have this:

let jsonobject = {   email: "useremail",   password: "userpassword", } 

i think can

jsonobject['grant_type'] = password; 

otherwise, simple , effective:

let extendedjsonobject = {   email: jsonobject.email,   password: jsonobject.password,   grant_type: 'password' }; 

as @jonrsharpe said in comments, can so:

let extendedjsonobject = { grant_type: 'password', ...jsonobject } 

according @kyrsberg can

let extendedjsonobject = object.assign({grant_type: 'password'}, jsonobject); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -