Good morning
I am wanting to pass a string as a functions parameter in java script but the string will represent the name of a property that i want the function to operate on. I have seen this done before but don't quite comprehend it.
the function below shows what i'm referring to with the "field" parameter. it's passed a value as a string but operates on the property who's name matched the value of the string.
What i want to do is cycle through the array of objects and return only the values stored in the property who's name matches the string passed. The idea is to have one function which can process any objects with properties that have been added to an array and return any property without having to write a loop function for each property.
Below is an example of this type of magic:
listName.sort(sort_by('stringPropertyName', false, function(a){return a.toUpperCase()}));
var sort_by = function(field, reverse, primer){ //http://stackoverflow.com/questions/979256/how-to-sort-an-array-of-javascript-objects
var key = function(x){return primer ? primer(x[field]) : x[field]};
return function (a,b){
var A = key(a), B = key(b);
return ((A < B) ? -1 :(A > B) ? +1 : 0) * [-1,1][+!!reverse];
}
}