Seeking greater efficiency:
Hello:
I've a problem with jQuery.grep and finding a more efficient way of processing array(s). Details of the situation follow:
Sometime ago I created an address book, in which users can manage their business contacts and email addresses. The following capabilities exist with this address book table (dynamically populated).
- $("#select_all").click(function() {: Select All button (checks the checkbox for every address book contact)
- $('.Record_Chk').click(function() {: Depending on whether user checks (or unchecks) the checkbox, the routine adds or subtracts addressbook information from multiple arrays.
Following are the two routines:
- //================== Assemble data arrays for processing
- // Following function assembles row_id and email address arrays containing ALL contacts. The arrays are scrubbed of duplicate values before closing out the function.
- $("#select_all").click(function() {
- $('.Record_Chk').prop('checked',true);
- var ID_array = new Array();
- var e_mail_array = new Array();
- var r_owner_array = new Array();
- var r_contact_array = new Array();
-
- // Process the row_ID and row email address from all address book records into usable arrays.
- jQuery.each(cref_array, function(index, value)
- {
- var row_ID = "" + value + "";
- ID_array.push(row_ID);
- });
-
- jQuery.each(mail_array, function(index, value)
- {
- var email = "" + value + "";
- e_mail_array.push(email);
- });
-
- jQuery.each(rOwner_array, function(index, value)
- {
- var rOwner = "" + value + "";
- r_owner_array.push(rOwner);
- });
-
- jQuery.each(rContact_array, function(index, value)
- {
- var rContact = "" + value + "";
- r_contact_array.push(rContact);
- });
- // Now make sure your arrays have unique values.
- rowID_array = removeDuplicateElement(ID_array);
- email_array = removeDuplicateElement(e_mail_array);
- rowOwner_array = removeDuplicateElement(r_owner_array);
- rowContact_array = removeDuplicateElement(r_contact_array);
- });
-
- //================= Process data arrays after user clicks SELECT checkbox ======================
- // If a user checks or unchecks the checkbox, then appropriate values are added to (or removed from) data arrays.
- $('.Record_Chk').click(function() {
- var check = $(this).is(':checked');
- var catch_array = jQuery.trim($(this).val());
- var data_row = jQuery.parseJSON(catch_array);
- console.log("Data Row: " + data_row);
- var row_ID = data_row.CrossRefID;
- var row_email = data_row.EmailAddress;
- var row_owner = data_row.AddressOwner;
- var row_contact = data_row.LName + ", " + data_row.FName
- if (check == true)
- {
- rowID_array.push(row_ID); //rowID_array houses all selected RowIDs. Can use this info to run delete queries.
- email_array.push(row_email); //email_array houses all selected emails. Can use info to send emails.
- rowOwner_array.push(row_owner);
- rowContact_array.push(row_contact);
- console.log("Row Contact Array Add: " + rowContact_array);
- }
- else
- {
- var removeItem = row_ID;
- rowID_array = jQuery.grep(rowID_array, function(value) {
- return value != removeItem; });
-
- var removeEmail = row_email;
- email_array = jQuery.grep(email_array, function(value) {
- return value != removeEmail; });
-
- var removeOwner = row_owner;
- rowOwner_array = jQuery.grep(rowOwner_array, function(value) {
- return value != removeOwner; });
-
- var removeContact = row_contact;
- rowContact_array = jQuery.grep(rowContact_array, function(value) {
- return value != removeContact;});
- }
- });
The largest inefficiency is that I'm assembling multiple arrays of the same data set. The reason I built it this way is because I can not figure out how to handle multidimensional arrays with jQuery.grep.
Not only is this inefficient (in vb I would never construct something like this), but this way of handling the data is causing real-life problems. Since multiple rows of data can have the same first name, jQuery.grep is removing legitimate duplicates of multiple first names.
If the first and last names were being processed in a multidimensional array (along with ID numbers and email addresses) then jQuery.grep wouldn't remove 3 "John" first names after the user selects "John Doe".
I hope the description of my problem is coherent. There has to be a better way to handle this. I know how to process multidimensional arrays once they reach php. In a straight-forward jQuery application I know how to construct a multidimensional array.
But... in this application where a user can move back and forth between selecting all the contacts, unchecking some contacts, and re-checking other contacts, I have to find a way to build a multidimensional array that can remove a whole sub-array of data from the main multidimensional array. That is where I'm stumped.
Any advice is welcome.
Thanks in advance - Pavilion