-2

I am getting object in response of axios/ajax request. Now i want to add key value pair in that object how i Can do this.

Here is the object

user: {id: 1, name: "swifty", email: "swift.solutions.com@gmail.com", avatar: null, email_verified_at: null,…}

I want to update this object and add one more key is_customer

This is how i tried But it assigned only one key value is_customer and removed all others

response.data.user = {'is_customer': is_customer}
Dan
  • 59,490
  • 13
  • 101
  • 110
Bilal Arshad
  • 531
  • 3
  • 11
  • 33
  • 1
    `response.data.user.is_customer = is_customer` – Jon Jan 07 '21 at 06:12
  • 1
    Does this answer your question? [How can I add a key/value pair to a JavaScript object?](https://stackoverflow.com/questions/1168807/how-can-i-add-a-key-value-pair-to-a-javascript-object) – Omkar76 Jan 07 '21 at 06:15

1 Answers1

1
user.is_customer = is_customer;

or

user["is_customer"] = is_customer;

Both are the same thing, but make sure user itself is defined so that these two don't throw errors when ran.

user in this case can be any object.

BGPHiJACK
  • 1,277
  • 1
  • 8
  • 16