How can I call a function with an object and an array and add it to a property of that object?

Samantha JT Star

I have a Javascript test application using Selenium Web Driver. When setting up code ready to test, I always use code similar to the following code that sets properties before testing the page. In this case it's the theme page, so I have the following code to set the theme object and then add properties to it:

var topic = this;

topic.topicName = element('topicName'));
topic.topicVersion = element('topicVersion'));
topic.description = element('description'));
topic.topicHeader = element('topicHeader'));
topic.topicFooter = element('topicFooter'));

Is there a way to simplify this setup so I don't have to code the calls to it multiple times element( Ideally I just want to pass an object and an array of different fields and execute some code and element( add properties to that object? Note that the attribute name I want to have is the one that matches the element ID.

I'll give some tips on how to do this, any help would be greatly appreciated.

Denys Séguret

You can do this:

['topicName', 'topicVersion', ...].forEach(function(k){
    topic[k] = element(;