最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass string to function to define property name - Stack Overflow

programmeradmin3浏览0评论

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 prehend 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){ //
   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];                  
   }
}

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 prehend 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./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];                  
   }
}
Share Improve this question asked Oct 3, 2012 at 15:17 ReahreicReahreic 6442 gold badges9 silver badges33 bronze badges 2
  • 2 What exactly is the question ? – Denys Séguret Commented Oct 3, 2012 at 15:17
  • i wish to understand how one goes about using a string to represent a property name. – Reahreic Commented Oct 3, 2012 at 15:24
Add a ment  | 

1 Answer 1

Reset to default 7

If you want to "cycle through the array of objects and return only the values stored in the property who's name matches the string passed", you may do this :

function getValues(array, propname) {
     var values = [];
     for (var i=0; i<array.length; i++) {
         if (typeof array[i][propname] !== 'undefined') {
             values.push(array[i][propname])
         }
     }
     return values;
}

The "trick" is to access the property using obj[propname] instead of obj.propname when propname is a variable containing the name of the property.

For example window.location can be accessed as window["location"]

DEMONSTRATION

发布评论

评论列表(0)

  1. 暂无评论