Question on efficiency of script
Hello:
I've just finished writing a routine to do the following:
- Use $.post to grab a dynamically produced php table and display it in a "group" container. The table shows 1 line for each group member, and their privileges.
- Use $(".grpBK_O1 select").map(function(){ to iterate through the resulting member table and grab the value of the privileges <select object.
- Assign values to variables that will be pushed into an array
- Use $.each(memberID, function() { to test and process EVERY memberID
- Use $.grep(grpPrivilege_array, function(v,i) { within the $.each() to figure out if there are duplicates in the array - BEFORE pushing
- Use if (result=='') to do an array push
What bothers me - from an efficiency perspective - is how many different procedures I'm using to accomplish this. Following is the actual script:
- if (grpCheck == true)
- {
- // Open Group detail section so user can assign privileges
- // ### grab data (group members and privileges) and display in detail section
- $.post("ol_list_grpMembers_O1.php",{bind_grpID:grp_ID},function(data){
- $(grpContainer).removeClass('ol_hidden').addClass('ol_shownTR');
- $(grpContainer + ">td").html(data);
- // iterate through the <select objects
- values = $(".grpBK_O1 select").map(function(){
- // now set variables for assembling the array
- row_ID = this.id; // this.id is jQuery for javascripts: $(this).attr('id');
- memberID = row_ID.split("_").pop(-1);
- grpPrivilegesID = $(this).val();
- // Use $.each to test each memberID against grpPrivilege_array for possible duplicates.
- $.each(memberID, function() {
- // Use Grep to find duplicates.
- var result = $.grep(grpPrivilege_array, function(v,i) {
- return v['participantID'] === memberID;
- });
- // If the grep result is empty '', then push memberID and grpPrivilegesID into grpPrivilege_array
- if (result=='')
- {
- grpPrivilege_array.push({participantID:memberID, PrivID:grpPrivilegesID});
- }
- });
- return grpPrivilege_array;
- }).get();
- console.log("result array: " + JSON.stringify(grpPrivilege_array));
- });
-
- // Set background color of row
- $(this).closest('tr').css('background-color', '#85a888');
- }
jQuery is newish to me - but I've decades of experience in other languages. And it is a bit concerning to me that I'm nesting so many different types of functions to accomplish the goals outlined above.
Is there a more efficient way to accomplish my goals?
Thanks in advance: Pavilion