-
Nov 16th, 2015, 02:35 PM
#1
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()
-
Nov 16th, 2015, 04:05 PM
#2
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
-
Nov 16th, 2015, 07:01 PM
#3
Re: JQuery - Get Item in Array by Attribute
There are polyfills available for ECMAScript 5 if you still need to support IE8.
-
Nov 17th, 2015, 09:36 AM
#4
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?
-
Nov 17th, 2015, 11:19 AM
#5
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.
-
Nov 17th, 2015, 11:32 AM
#6
Re: JQuery - Get Item in Array by Attribute
Have you thought about making a bigger class that manages the array and what not?
-
Nov 17th, 2015, 03:50 PM
#7
Re: JQuery - Get Item in Array by Attribute
I'm not sure that I understand, what do you mean?
-
Nov 17th, 2015, 04:11 PM
#8
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);
}
-
Nov 18th, 2015, 12:52 AM
#9
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.
-
Nov 18th, 2015, 05:04 PM
#10
Re: JQuery - Get Item in Array by Attribute
I went with Penagate's KISS solution and it works well. Thanks y'all!
-
Nov 18th, 2015, 05:23 PM
#11
Re: JQuery - Get Item in Array by Attribute
@penagate - I've never seen that ++i syntax - what's up with that?
-
Nov 18th, 2015, 06:41 PM
#12
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.
-
Nov 18th, 2015, 07:49 PM
#13
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++
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|