数组比较

发布于 2024-09-25 04:14:19 字数 5140 浏览 2 评论 0原文

我正在尝试根据选择创建一个唯一的任务ID 列表。每个任务最多可以有2个公司。我的问题是下面的任务 ID 比较失败。我的代码:

<script>
$().ready(function() { 

  $.validator.addMethod(
        "findRegex",
        function(value, element) {
            return /^.+::[1-9]\d{0,11}$/.test(value);
        },
        "Please check your input."
   );

    $('#myForm').validate({
            submitHandler: function(form){
                $("#formSub").html('<table style="font-size:11px; color:#333;"><tr><td><img border="0" src="images/ajax-loader.gif"/></td><td>Saving! Please wait...</td></table>');
                var options = { 

                    success: showResponse,
                    url:'addValidation2.cfm?t=1' 
                };
                $('#myForm').ajaxSubmit(options);
                return false;   
            }
    });

      $('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 = [];
            var arr3 = [];
            if (currTaskIDs != '') {
                if( $.inArray(currTaskIDs, arr2) == -1) {
                    arr2.push(currTaskIDs);
                }
            } 

            if( $.inArray(tid, arr) == -1) {
                arr.push(tid);
            } 

            arr3 = unionArr(arr,arr2);
            alert(arr3);
            $("#taskIDList").val(arr3);
            // end
        });

        unionArr = function(x, y) {
          var obj = {};
          for (var i = x.length-1; i >= 0; -- i)
             obj[x[i]] = x[i];
          for (var i = y.length-1; i >= 0; -- i)
             obj[y[i]] = y[i];
          var res = []
          for (var k in obj) {
            if (obj.hasOwnProperty(k))  
              res.push(obj[k]);
          }
          return res;
        }


    $("input[name^=compName-]").each(function() {
       $(this).rules("add", { findRegex: true });
    });
});
 </script>

<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>

  1. 我为任务 35 选择两家公司。taskIDList 文本框显示 35。 <通过>
  2. 我为任务 36 选择两家公司,taskIdList 文本框显示: 36,36,35 <失败> 它应该显示 36、35。
  3. 我为任务 37 选择一家公司,taskIdList 正确地将 37 添加到列表中。然后我换了公司,那么taskIdList就变成了37,37,36,36,35:<失败>

我如何确保该列表是唯一的?

提前致谢

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:

<script>
$().ready(function() { 

  $.validator.addMethod(
        "findRegex",
        function(value, element) {
            return /^.+::[1-9]\d{0,11}$/.test(value);
        },
        "Please check your input."
   );

    $('#myForm').validate({
            submitHandler: function(form){
                $("#formSub").html('<table style="font-size:11px; color:#333;"><tr><td><img border="0" src="images/ajax-loader.gif"/></td><td>Saving! Please wait...</td></table>');
                var options = { 

                    success: showResponse,
                    url:'addValidation2.cfm?t=1' 
                };
                $('#myForm').ajaxSubmit(options);
                return false;   
            }
    });

      $('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 = [];
            var arr3 = [];
            if (currTaskIDs != '') {
                if( $.inArray(currTaskIDs, arr2) == -1) {
                    arr2.push(currTaskIDs);
                }
            } 

            if( $.inArray(tid, arr) == -1) {
                arr.push(tid);
            } 

            arr3 = unionArr(arr,arr2);
            alert(arr3);
            $("#taskIDList").val(arr3);
            // end
        });

        unionArr = function(x, y) {
          var obj = {};
          for (var i = x.length-1; i >= 0; -- i)
             obj[x[i]] = x[i];
          for (var i = y.length-1; i >= 0; -- i)
             obj[y[i]] = y[i];
          var res = []
          for (var k in obj) {
            if (obj.hasOwnProperty(k))  
              res.push(obj[k]);
          }
          return res;
        }


    $("input[name^=compName-]").each(function() {
       $(this).rules("add", { findRegex: true });
    });
});
 </script>

<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>

  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 ?

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑叹一世浮沉 2024-10-02 04:14:19

将此视为简单地从最终列表中删除重复项的问题,您可以编写一个小函数,如 这个答案。搁置任何 担心修改您不拥有的对象来添加此功能(事实上它是 O(n^2)),该函数可以让您执行此操作:

var theArray = [37,37,36,36,35];
var theUniqueArray = theArray.unique(); // -> [37,36,35]

编辑:我修复了你的小提琴。你有一堆问题。请查看此处

有几件事:

  • 您不需要在 jsFiddle 中添加
  • 您不需要添加 $(document).ready(...)围绕 jsFiddle 中的代码。
  • 由于 Array.prototype.unique = function()... 是一个函数表达式,因此它不会被提升。我把它移到了你的其他代码之上。
  • 您正在从零循环到 currTaskIDs 的长度,它是一个字符串,而不是一个数组。因此,当其值为 35,​​36 时,即为五次迭代,而不是两次。由于 unique 函数对数组进行操作,因此我已将字符串更改为数组:

    if (currTaskIDs.length) arr = currTaskIDs.split(',');
    
  • 接下来,我们将当前选定的值附加到数组(如果适用):

    if (tid.length) arr.push(tid);
    
  • 最后,我们运行启动整个练习的 unique 函数,然后将整个混乱情况恢复原样放入字符串并将其放入您的字段中:

    var u = arr.unique();
    $("#taskIDList").val(u.join(','));
    

Looking at this as a problem of simply removing duplicates from your final list, you can write a little function like the one shown in this SO answer. Putting aside any concerns with modifying objects you don't own to add this functionality (and the fact that it's O(n^2)), the function lets you do this:

var theArray = [37,37,36,36,35];
var theUniqueArray = theArray.unique(); // -> [37,36,35]

Edit: I fixed up your fiddle. You had a bunch of issues. Take a look here.

A few things:

  • You don't need to add <script> tags in jsFiddle
  • You don't need to add $(document).ready(...) around your code in jsFiddle.
  • Since Array.prototype.unique = function()... is a function expression, it is not hoisted. I moved it above your other code.
  • You're looping from zero to the length of currTaskIDs, which is a string, not an array. Thus when its value is 35,36, that's five iterations, not two. Since the unique function operates on arrays, I've changed the string to an array:

    if (currTaskIDs.length) arr = currTaskIDs.split(',');
    
  • Next we append the currently selected value to the array, if applicable:

    if (tid.length) arr.push(tid);
    
  • Finally we run the unique function that started this whole exercise, then turn the whole mess back into a string and plop it into your field:

    var u = arr.unique();
    $("#taskIDList").val(u.join(','));
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文