Is there a way to dynamically create objects in JavaScript?
My Expectation
var tree_data = {
'for-sale': {name: 'For Sale', type: 'folder'},
'vehicles': {name: 'Vehicles', type: 'folder'},
'rentals': {name: 'Rentals', type: 'folder'},
'real-estate': {name: 'Real Estate', type: 'folder'},
'pets': {name: 'Pets', type: 'folder'},
'tickets': {name: 'Tickets', type: 'item'},
'services': {name: 'Services', type: 'item'},
'personals': {name: 'Personals', type: 'item'}
}
My Attempt
// Removed all white spaces from the string
var name = result[index].Text.replace(/ /g, '');
// Created the item
var newMenu = {
folder: {
name: result[index].Text,
type: "folder"
}
}
// Populated the object
self.tree_data.push(newMenu);
However, my tree_data object ends up looking like this
{
folder: {
name: "For Sale',
type: 'Folder'
}
}
The key folder
should be the same as the name
without whitespace. How can I achieve this in JavaScript?