I'm trying to find the best way to abstract away these three functions into one.
function displaySummary(data) {
getMyElement('app__summary').innerHTML = data.summary;
return data;
}
function displayTemperature(data) {
getMyElement('app__temperature').innerHTML = data.temperature;
return data;
}
function displayLocation(data) {
getMyElement('app__location').innerHTML = data.location;
return data;
}
As you can see, the three of them share the exact structure. The parameter is just a simple object with some properties.
What I would like to do is to be able to call one function with two arguments like so:
displayElement(data, 'location');
And have something like this run (note, this next one is pseudocode):
function displayElement(data, STRING) {
getMyElement('app__STRING').innerHTML = data.STRING;
return data;
}
How would you go about doing this? ES2015 syntax is welcome too.
META: I'm aware that this question might be flagged as a potential duplicate of the questions below. Neither of these answer how to interpolate (if at all possible) a string to work as a property accessor, which is mainly how I 'think' this problem should be solved. I'm open to suggestions and edits to make this question a better one.
EDIT: @Bergi flagged as duplicate a question that didn't come in my search and that does solve this question. Should I delete this one?