我的数组数据被我的自定义(集合论)Complements() 函数以某种方式损坏了?

发布于 2024-10-07 12:36:45 字数 3553 浏览 0 评论 0原文

我厌倦了有限的 javascript 数组函数,想编写一些自己方便的原型函数来执行集合论函数。

下面是我到目前为止的代码

<script type="text/javascript">

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

Array.prototype.getIndices = function(obj){
    var indices = new Array();
    var i = this.length;
    while (i--) {
        if(this[i] === obj){
            indices.push(i);
        }
    }
    return indices;
}

Array.prototype.Union = function(arr){
    //combines two arrays together to return a single array containing all elements (once)
    //{1,2,3,4,5}.Union({3,4,5,6,7})
    //returns: {1,2,3,4,5,6,7}
        var primArray = this;
        var secondArray = arr;
    var i = primArray.length;
    while(i--){
        if(secondArray.contains(primArray[i])){
            primArray.splice(i, 1);
        }
    }
    var returnArr = primArray.concat(secondArray);
    return returnArr;
}

Array.prototype.Intersection = function(arr){
    //Returns an array of elements that are present in both sets
    //{1,2,3,4,5}.Intersection({3,4,5,6,7})
    //returns: {3,4,5}
        var primArray = this;
        var secondArray = arr;
    var returnArr = new Array;
    var i = 0;
    while(i++<primArray.length){
        if(secondArray.contains(primArray[i])){
            returnArr.push(primArray[i]);
        }
    }
    return returnArr;
}

Array.prototype.Complement = function(arr){
    //Returns an array of elements that are only in the primary (calling) element
    //{1,2,3,4,5}.Complement({3,4,5,6,7})
    //return: {1,2}
        var primArray = this;
        var secondArray = arr;
    var i = primArray.length;
    while(i--){
        if(secondArray.contains(primArray[i])){
            primArray.splice(i, 1);
        }
    }
    return primArray;
}

Array.prototype.SymmetricDifference = function(arr){
    //Returns elements that are exclusive to each set
    //{1,2,3,4,5}.SymmetricDifference({3,4,5,6,7})
    //return: {1,2,6,7}
        var primArray = this;
        var secondArray = arr;
    var i = primArray.length;
    while(i--){
        if(secondArray.contains(primArray[i])){
            var indices = secondArray.getIndices(primArray[i]);
            primArray.splice(i, 1);
            var j=indices.length;
            while(j--){
                secondArray.splice(indices[j], 1);
            }
        }
    }
    var returnArr = primArray.concat(arr);
    return returnArr;
}

function run(){
     var Q = "A";
     var D = [1,2,3,4,5,6,7,8,9,10];
     var sets = {
          "A":[1,2,3],
          "B":[3,4,5],
          "C":[5,6,7]
     }
     var R = D;
     for(var el in sets){
          R = R.Complement(sets[el]);
     }
//if I alert D at this point I get 8,9,10 instead of 1,2,3,4,5,6,7,8,9,10 as I would expect? What am I missing here... It causes a problem when I perform D.Complement(R) later on
     document.write(R + "<br/>");
     R = R.Union(sets[Q]);
     document.write(R + "<br/>");
  //Here!  
     R = D.Complement(R);
     document.write(R);
}

</script>

</head>

<body onload="run()">

</body>
</html>

当我尝试获得域的补集和我新构建的集合时,一切都在进行到最后一点。我预计会得到 [1,2,3,4,5,6,7,8,9,10] 和 [8,9,10,1,2,3] 的补集,这将产生 [4, 5,6,7] 但当我执行 D.Complement(R) 时,我的 D 变量似乎变成了 [1,2,3]。这似乎是在我执行枚举之后发生的。

我想这可能是因为我在函数中使用了 this.splice 和 arr.splice ,当我将变量传递给函数时,它们被作为指针传递,这意味着我实际上正在处理实际的内存位置。所以我然后使用 primArray 和 secondaryArray 创建一个副本来处理...但问题仍然发生

非常感谢

I was fed up with the limited javascript Array functions and wanted to write a few of my own handy prototype functions to perform Set Theory functions.

Below is the code I have for this so far

<script type="text/javascript">

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

Array.prototype.getIndices = function(obj){
    var indices = new Array();
    var i = this.length;
    while (i--) {
        if(this[i] === obj){
            indices.push(i);
        }
    }
    return indices;
}

Array.prototype.Union = function(arr){
    //combines two arrays together to return a single array containing all elements (once)
    //{1,2,3,4,5}.Union({3,4,5,6,7})
    //returns: {1,2,3,4,5,6,7}
        var primArray = this;
        var secondArray = arr;
    var i = primArray.length;
    while(i--){
        if(secondArray.contains(primArray[i])){
            primArray.splice(i, 1);
        }
    }
    var returnArr = primArray.concat(secondArray);
    return returnArr;
}

Array.prototype.Intersection = function(arr){
    //Returns an array of elements that are present in both sets
    //{1,2,3,4,5}.Intersection({3,4,5,6,7})
    //returns: {3,4,5}
        var primArray = this;
        var secondArray = arr;
    var returnArr = new Array;
    var i = 0;
    while(i++<primArray.length){
        if(secondArray.contains(primArray[i])){
            returnArr.push(primArray[i]);
        }
    }
    return returnArr;
}

Array.prototype.Complement = function(arr){
    //Returns an array of elements that are only in the primary (calling) element
    //{1,2,3,4,5}.Complement({3,4,5,6,7})
    //return: {1,2}
        var primArray = this;
        var secondArray = arr;
    var i = primArray.length;
    while(i--){
        if(secondArray.contains(primArray[i])){
            primArray.splice(i, 1);
        }
    }
    return primArray;
}

Array.prototype.SymmetricDifference = function(arr){
    //Returns elements that are exclusive to each set
    //{1,2,3,4,5}.SymmetricDifference({3,4,5,6,7})
    //return: {1,2,6,7}
        var primArray = this;
        var secondArray = arr;
    var i = primArray.length;
    while(i--){
        if(secondArray.contains(primArray[i])){
            var indices = secondArray.getIndices(primArray[i]);
            primArray.splice(i, 1);
            var j=indices.length;
            while(j--){
                secondArray.splice(indices[j], 1);
            }
        }
    }
    var returnArr = primArray.concat(arr);
    return returnArr;
}

function run(){
     var Q = "A";
     var D = [1,2,3,4,5,6,7,8,9,10];
     var sets = {
          "A":[1,2,3],
          "B":[3,4,5],
          "C":[5,6,7]
     }
     var R = D;
     for(var el in sets){
          R = R.Complement(sets[el]);
     }
//if I alert D at this point I get 8,9,10 instead of 1,2,3,4,5,6,7,8,9,10 as I would expect? What am I missing here... It causes a problem when I perform D.Complement(R) later on
     document.write(R + "<br/>");
     R = R.Union(sets[Q]);
     document.write(R + "<br/>");
  //Here!  
     R = D.Complement(R);
     document.write(R);
}

</script>

</head>

<body onload="run()">

</body>
</html>

Everything is working up to the final point when I then try to get the complement of the domain and my newly constructed set. I am expected to be getting the complement of [1,2,3,4,5,6,7,8,9,10] and [8,9,10,1,2,3] which would yield [4,5,6,7] but when I perform D.Complement(R) my D variable seems to have turned into [1,2,3]. This appears to happen after the enumeration I perform.

I thought it might be because I was using this.splice and arr.splice in my functions and when I was passing the variables to the functions they were being passed as pointers meaning I was actually working on the actual memory locations. So I then used primArray and secondArray to create a duplicate to work on... but the problem is still happening

Many Thanks

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

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

发布评论

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

评论(1

缪败 2024-10-14 12:36:45

所以我然后使用 primArray 和 secondaryArray 创建一个副本来处理......但问题仍然发生

只是将其分配给一个变量并不会使它成为一个新数组,您仍在处理传入的数组。您必须通过循环遍历数组并复制每个索引或通过连接和拆分来手动创建数组的新副本。

So I then used primArray and secondArray to create a duplicate to work on... but the problem is still happening

Just assigning it to a variable does not make it a new array, you are still working on the array that was passed in. You have to manually make a new copy of the array either by looping through it and copy each index or by joining and splitting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文