user160820
user160820

Reputation: 15230

How do I add a new key:value pair in existing JavaScript object?

I have a JavaScript object like:

appointerment= {ids: '15,16,17', appointments: {'15': '12.05.2010,14,05,2010'} }

now in appointments object I want to add something like '16': '21.05.2010'

what is the best possible way to do this?

Upvotes: 1

Views: 6196

Answers (3)

vsync
vsync

Reputation: 130750

var x = { a:1, b:{c:1} }

x.b.d = 1

// x is now { a:1, b:{c:1, d:1} }

Note - But this won't work in the case of numbers like '16'.

Upvotes: 0

Jakub Hampl
Jakub Hampl

Reputation: 40573

appoiterment['appointments']['16'] = '21.05.2010';

Upvotes: 0

Amber
Amber

Reputation: 527478

appointerment.appointments['16'] = '21.05.2010';

JSON is short for "JavaScript Object Notation", and as the name implies, it's basically just a way of representing Javascript objects. Thus, one can interact with JSON in the ways one tends to interact with any other object in Javascript, via the usage of the . or [] operators.

Upvotes: 6

Related Questions