为什么我在二维数组中拆分特定数组,但它也拆分了另一个数组

时间:2016-08-20 07:44:10

标签: javascript arrays

enter image description here

我之前从未遇到过这个问题,而且我真的不知道这是怎么回事。有人能为我解释一下吗?

还有一个图像,它位于顶部。正如您在图片中看到的那样,最后一个数组在其他数组发生更改时没有发生变化。

这是方法(我正面临着我的问题)

    Object.prototype.createObject = function(name){
            this[name] = [];
        }
        Array.prototype.matrixCreator = function(a3,b3){
            var length = this.length;
            if((this[1][0] && this[0][1]) === undefined){
                var array1 = new Array(length-1);
                for(var d3=0;d3<length-1;d3++){
                    array1[d3] = new Array(length-1);
                }
            }else{
                var array = new Array(length);
                for(var e3=0;e3<length;e3++){
                    array[e3] = new Array(length);
                }
            }
            for(var c3=0;c3<length;c3++){
                this[c3].splice(b3,1);
                document.writeln(this[c3]);
                if(c3 === a3){              // this prototype doesn't support the new design of matrix about spliting
                if(c3===0){
                    this.splice(a3,1);
                    length--;
                }else{
                    this.splice(a3,1);
                    c3--;
                    length--;
                }
                }
            }
            var f3;
            var g3;
            if((this[1][0] && this[0][1]) === undefined){
                array1[0][0] = this[a][b];
                for(var h3=1;h3<length;h3++){
                    for(var i3=1;i3<length;i3++){
                        array1[h3][i3] = this[h3][i3];
                    }
                }
                return array1;
            }else{
            array[0][0] = this[a3][b3];
            for(var j3=1;j3<length+1;j3++){
                for(var l3=1;l3<length+1;l3++){
                    f3 = j3-1;
                    g3 = l3-1;
                    array[j3][l3] = this[f3][g3];
                }
            }
            return array;
            }
            }
 // Here is the test code for my method:

var button = document.getElementById("button");
    button.addEventListener("click",function(){
            var array10 = new Array(4);
            for(var b=0;b<4;b++){
                array10[b] = [];
            }
            var array3 = [1,2,3,4];
            var array4 = [5,6,7,8];
            for(var c=0;c<4;c++){
                array10[c]=array3;
                array10[c+1]=array4;
            }
            var n = array10.length;
            var controller = -1;
            var key=0;var key1=0;
            var obj ={};
            var array=[]; var array1=[];
            var flow=0;
            while(key1 === 0){
                for(var a=0;a<n;a++){
                    var string = "array" + a;
                    if(key==0){
                        obj.createObject(string);
                        array = array10.matrixCreator(0,a);
                        obj[string].push(array);
                        document.writeln(obj[string]);
                    }else{
                        for(var d=1;d<flow;d++){
                            array = obj[string][controller].matrixCreator(1,d);
                            obj[string].push(array);
                            document.writeln(obj[string]);
                        }
                    }
                }
                    key=1;
                    controller++;
                    flow = obj[string][controller].length;
                    if(flow === 3){
                        break;
                    }
            }
    });

html代码:

<input type="button" id="button" value="press here"/>

0 个答案:

没有答案