Results 1 to 13 of 13

Thread: JQuery - Get Item in Array by Attribute

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    JQuery - Get Item in Array by Attribute

    So I have the following class:
    Code:
    function Driver() {
    	this.FirstName = "First Name";
    	this.MiddleInitial = "Middle Initial";
    	this.LastName = "Last Name";
    	this.Relationship = "Relationship";
    	this.DateofBirth = "Date of Birth";
    	this.DriversLicense = "Drivers License";
    	this.ID = "-1";
    }
    What I'm wanting to do is get a class based on some values. So what I'd like to do is get an item by it's FirstName and LastName properties. I know in VB.Net it'd be something like this:
    Code:
    Dim d As Driver = (From dr As Driver In Drivers Where dr.FirstName = "Some Value" AndAlso dr.LastName = "Some Other Value").FirstOrDefault()
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: JQuery - Get Item in Array by Attribute

    This is untested, but couldn't you do something like this?

    Code:
    var driversFound = drivers.filter(function(driver){
      return driver.FirstName === "aaa" && driver.LastName === "bb";
    });
    However, the above doesn't work in IE8 I believe.
    Last edited by kfcSmitty; Nov 16th, 2015 at 07:05 PM. Reason: fixed typo

  3. #3
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: JQuery - Get Item in Array by Attribute

    There are polyfills available for ECMAScript 5 if you still need to support IE8.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: JQuery - Get Item in Array by Attribute

    The jQuery EACH function will loops through a list of items.

    Are these all in an array? Do you want to return an array of matching items?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: JQuery - Get Item in Array by Attribute

    All of them are in an array but I only want to return a single instance of a driver who's first and last name match some value.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: JQuery - Get Item in Array by Attribute

    Have you thought about making a bigger class that manages the array and what not?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: JQuery - Get Item in Array by Attribute

    I'm not sure that I understand, what do you mean?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: JQuery - Get Item in Array by Attribute

    If you are going to be doing that filter code in a variety of JavaScript functions I would make that logic part of class - with like a .SetDriver() method that does just this filtering. Then that class can retain the "selected" driver and you have other methods to work that driver.

    Here is a grid_alert class I just started working on that will handle ALERT's from long polling to the server.

    It currently has one "internal / private" function (i_ajax()) and it returns out a couple of EXPOSED methods (the poll_server method is being trashed).

    Code:
            var grid_alert = function () {
                var gaCycle = 30000;
                var i_ajax = function () {
                    setTimeout(function () {
                        ctrlWebService("updatealert", "check", "", "", "", []);
                        i_ajax();
                    }, gaCycle);
                };
                ctrlWebService("updatealert", "init", "", "", "", []);
                i_ajax();
                return {
                    poll_server: function () {
                        i_ajax();
                    },
                    register_alert: function (ddtype, value) {
                        ctrlWebService("updatealert", "register", ddtype, value, "", []);
                    }
                };
            };
    It is instantiated once like this - the var up top makes it a global variable (on the WINDOW object) but I instantiate it later on in another AJAX call if it's required.

    Code:
            var g_GAWorker;
    .
    .
    .
    g_GAWorker = new grid_alert();
    and then later on I use it like this

    Code:
        if (objGrid.awcoptions.updatealert) {
            g_GAWorker.register_alert(objGrid.awcoptions.updateddtype, objGrid.awcoptions.updatevalue);
        }

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: JQuery - Get Item in Array by Attribute

    If number of drivers is not many (<500, say) then KISS

    Code:
    // If 'drivers' is an array containing Driver instances
    function get_driver_firstlast(first, last) {
      for (var i = 0; i < drivers.length; ++i) {
        if (drivers[i].FirstName === first && drivers[i].LastName === last) {
          return drivers[i];
        }
      }
      return null;
    }
    If many drivers, consider creating an index structure which is a map of last name to list of drivers with that last name (or map of first name to list of drivers with that first name -- depending on whether your data tends to have more unique first names or last names).

    Then your lookup becomes:
    Code:
    function get_driver_firstlast(first, last) {
      var drivers = index[last];
      if (drivers) {
        for (var i = 0; i < drivers.length; ++i) {
          if (drivers[i].FirstName === first) return drivers[i];
        }
      }
      return null;
    }
    Because of the added complexity, avoid the index structure unless you find the first method to be too slow. If you have an index, at every point in your code where you modify the set of drivers, or modify a driver's first name or last name (whichever you use as the index field) you will need to add code to maintain the index.
    Last edited by penagate; Nov 18th, 2015 at 12:56 AM.

  10. #10

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: JQuery - Get Item in Array by Attribute

    I went with Penagate's KISS solution and it works well. Thanks y'all!
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: JQuery - Get Item in Array by Attribute

    @penagate - I've never seen that ++i syntax - what's up with that?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: JQuery - Get Item in Array by Attribute

    In a for loop, it doesn't make any difference although I've read articles saying is is technically faster than i++ because technically i++ creates a copy for the purposes of displaying the current value and displaying.

    If using it on a variable by itself, it increments the variable before actually using the variable.

    For example

    Code:
    var y = 0;
    alert(y++); // results in 0
    
    var y = 0;
    alert(++t); // results in 1
    I had thought this might cause the for loop to miss the 0 index when looping, but that doesn't appear to be the case.

  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: JQuery - Get Item in Array by Attribute

    Thanks for that info - helpful.

    I was kind of assuming that with the scripting nature of the language that it was some kind of precedence thing.

    I've never seen it - kind of dangerous in an example if someone starts thinking it's equivalent to i++

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width