Skip to content Skip to sidebar Skip to footer

Combine A Variable With Variable Value In Javascript

I am trying to do something like this in Angular javascript (a simplified code): var modelName = 'date'; if (attrs.hasOwnProperty('today')) { scope.modelName = new Date(); } I

Solution 1:

You can access properties of objects using square brackets.

var modelName = "date";

if (attrs.hasOwnProperty('today')) {
   scope[modelName] = new Date();
}

Post a Comment for "Combine A Variable With Variable Value In Javascript"