Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array Comparison

Status
Not open for further replies.

FALCONSEYE

Programmer
Jul 30, 2004
1,158
US
I am using jQuery. I am trying to create a unique taskID list based on the selections. Each task may have 2 companies at the most. My problem is the taskID comparison below fails. My code:

Code:
<script>
$().ready(function() { 
                   
          $('select[name^=compName]').change(function() {
                var tid = $(this).attr('name').split('-')[1];
                var j   = $(this).attr('name').split('-')[2];
                var currTaskIDs = $("#taskIDList").val();
                 
                // begin: create the task list:
                var arr  = [];
                var arr2 = [];
                
                if (currTaskIDs != '') {
                    for(var i=0; i<currTaskIDs.length; i++) {
                        if( $.inArray(currTaskIDs, arr2) == -1) {
                            arr2.push(currTaskIDs);
                        }
                    }
                } 
                
                if( $.inArray(tid, arr2) == -1) {
                    arr2.push(tid);
                } 
                
                arr = arr2.unique();

                $("#taskIDList").val(arr);
                // end
            });
          
           Array.prototype.unique =
              function() {
                var a = [];
                var l = this.length;
                for(var i=0; i<l; i++) {
                  for(var j=i+1; j<l; j++) {
                    if (this[i] === this[j])
                      j = ++i;
                  }
                  a.push(this[i]);
                }
                return a;
              };

});
</script>
</head>

<body>

<form id="myForm" name="myForm" method="post" action="">
    
    task : 35
    <select name="compName-35-1" id="compName-35-1">
        <option value=""></option>
        <option value="acme inc::30">acme inc::30</option>
        <option value="my company::54781">my company::54781</option>
        <option value="abc llc::42443">abc llc::42443</option>
        <option value="zzzz">zzzz</option>
    </select> 
    
    <select name="compName-35-2" id="compName-35-2" style="padding-left:20px;">
        <option value=""></option>
        <option value="acme inc::30">acme inc::30</option>
        <option value="my company::54781">my company::54781</option>
        <option value="abc llc::42443">abc llc::42443</option>
        <option value="zzzz">zzzz</option>
    </select> 
    <hr/>
    
      task : 36
    <select name="compName-36-1" id="compName-36-1">
        <option value=""></option>
        <option value="acme inc::30">acme inc::30</option>
        <option value="my company::54781">my company::54781</option>
        <option value="abc llc::42443">abc llc::42443</option>
        <option value="zzzz">zzzz</option>
    </select> 

    <select name="compName-36-2" id="compName-36-2" style="padding-left:20px;">
        <option value=""></option>
        <option value="acme inc::30">acme inc::30</option>
        <option value="my company::54781">my company::54781</option>
        <option value="abc llc::42443">abc llc::42443</option>
        <option value="zzzz">zzzz</option>
    </select> 
    <hr/>
    
      task : 37
    <select name="compName-37-1" id="compName-37-1">
        <option value=""></option>
        <option value="acme inc::30">acme inc::30</option>
        <option value="my company::54781">my company::54781</option>
        <option value="abc llc::42443">abc llc::42443</option>
        <option value="zzzz">zzzz</option>
    </select> 
    
    <select name="compName-37-2" id="compName-37-2" style="padding-left:20px;">
        <option value=""></option>
        <option value="acme inc::30">acme inc::30</option>
        <option value="my company::54781">my company::54781</option>
        <option value="abc llc::42443">abc llc::42443</option>
        <option value="zzzz">zzzz</option>
    </select> 

    <hr/>
    
    <input type="submit" name="btnSave" id="btnSave" value="Save"/>
    <hr/>
    <input type="text" name="taskIDList"  id="taskIDList" value="" />
    <div id="formSub"></div>
</form>

1. i select two companies for task 35. taskIDList textbox displays 35. < pass >
2. i select two companies for task 36, taskIdList textbox displays : 36,36,35 < fail > it should be displaying 36, 35.
3. i select one company for task 37, taskIdList correctly adds 37 to the list. Then I change the company, then the taskIdList becomes 37,37,36,36,35 : < fail >

how can i make sure that the list is unique ?

 
[0] First, the logic of unique method is completely off or, at least, I don't get it. (You can independently test that bit of the script, easily, and you won't get the desired result of "unique".)

[0.1] This is one of the correct implementations as I see it!
[tt]
Array.prototype.unique_rev = function() {
var a = [];
var l = this.length;
var b;
for(var i=0; i<l; i++) {
b=false;
for(var j=0; j<a.length; j++) {
if (this === a[j]) {
b=true;
break;
}
}
if (!b) {a.push(this);}
}
return a;
};
[/tt]
[1] The the establing of the initial arr2 is completely off. The points to note are
[1.1] #taskIDList should by design storing serialized unique entries array. (The components are already unique is doing right all along.)
[1.2] $("#taskIDList").val() is returning a string (serialized array). Hence your use of .length on that is completely other thing than what you think.
[1.3] Hence, the loop assigning the arr2 should be read like this.
[tt]
[red]/*[/red]
if (currTaskIDs != '') {
//etc etc
}
[red]*/[/red]
[blue]arr2=(currTaskIDs=='')?[]:currTaskIDs.split(',');[/blue]
[/tt]
[2] Then the checking tid is apt to be added to the unique list is already gurranteed by the conditional
[tt]
if( $.inArray(tid, arr2) == -1) {
arr2.push(tid);
}
[/tt]

[2.1] Hence, either
[2.1.1] you can dump the call to the .unique() method which will be redundant, or
[2.1.2] you simple leave out the conditional the push tid to arr2 and then use the .unique() method.
[tt]
//either this
if( $.inArray(tid, arr2) == -1) {
arr2.push(tid);
}
arr=arr2;
/*
//or this (if you feel the call to .unique() is more appealing.)
arr2.push(tid);
arr=arr2.unique();
*/
[/tt]
[2.2] Hence, you see the unique method is not strictly necessary.

[3] With these corrections, sensible display will result in taskIDList.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top