Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:51:44+00:00 2026-06-12T08:51:44+00:00

Good morning I am wanting to pass a string as a functions parameter in

  • 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 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];                  
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:51:45+00:00Added an answer on June 12, 2026 at 8:51 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good morning, I am currently developing a java web application that exposes a web
Good morning, I am the developer of a medium sized PDA application that will
Good morning gang. I have a jQuery Datepicker object that is prepoulated using a
Good Morning, I am developing a four column footer using pure CSS, but I
Good morning folks, I'm just that beginner in json, I have a formated data
Good Morning Stackoverflow-ka-teers, I am creating a raffle form that allows you to enter
Good morning! I'm programming a managment aplication that shows a task list using WPF
Good Morning, I have copied this code from somewere <script type=text/javascript> $(function() { setInterval(rotateImages(),
Good morning. I have a modal JQuery dialog that on Open calls and loads
Good Morning, I have a SharePoint site that I've been trying to fix up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.