Reputation: 459
Actually I am new to JavaScript, How to create JavaScript object with key and value pair dynamically? I want to create JavaScript object dynamically as mentioned below :-
var Categories = {
Education: {
name: 'Education',
models: ['Engineering', 'Arts', 'B.com'],
},
Medical: {
name: 'Medical',
models: ['Radiologist', 'Surgeon', 'Neurologist'],
},
Agriculture: {
name: 'Agriculture',
models: [ ' Domesticated', 'Bee keep', 'Live stock ', 'Orchards ',
'Organic farming '],
},
};
Upvotes: 2
Views: 182
Reputation: 5815
for creating json you can use two syntax :
Dot syntax:
var obj ={};
obj.someKnowValue = yourThing;
Bracket syntax :
var x = 'someDynamicString';
var obj = {};
obj[x] = yourThing;
So you can achieve it by using 2nd method where you will pass Education and all as variable to be a key.
Upvotes: 3