Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用自定义方法取消移位/移位单个值和多个值?_Javascript_Arrays_Prototype - Fatal编程技术网

Javascript 如何使用自定义方法取消移位/移位单个值和多个值?

Javascript 如何使用自定义方法取消移位/移位单个值和多个值?,javascript,arrays,prototype,Javascript,Arrays,Prototype,我有一些原型来重新创建数组方法的工作方式,如pop/push/shift/etc,我想扩展该功能以执行以下操作: 按下/弹出/移动/取消移动多个值 array.push(0); 阵列推送(1); 阵列推送(2); expect(array.pop()).to.be(2); expect(array.pop()).to.be(1); 期望(array.pop()).to.be(0); 推送/弹出/取消移动/etc单个值 array.push(0); 阵列推送(1); 期望值([0,1]);

我有一些原型来重新创建数组方法的工作方式,如pop/push/shift/etc,我想扩展该功能以执行以下操作:

  • 按下/弹出/移动/取消移动多个值
    
    array.push(0);
    阵列推送(1);
    阵列推送(2);
    expect(array.pop()).to.be(2);
    expect(array.pop()).to.be(1);
    期望(array.pop()).to.be(0);
    

  • 推送/弹出/取消移动/etc单个值
    
    array.push(0);
    阵列推送(1);
    期望值([0,1]);
    
    
    数组.pop(1);
    期望值([0]);
    

  • 我的假设是需要一个全局数组变量来存储元素。是这样吗

    这是我的密码:

    var mainArray = [];  // array no longer destroyed after fn() runs
    function YourArray(value) {
      this.arr = mainArray;  // looks to global for elements | function?
      this.index = 0;
      var l = mainArray.length;
    
      if(this.arr === 'undefined')
        mainArray += value;        //  add value if array is empty
      else
        for(var i = 0; i < l ; i++)             // check array length
            mainArray += mainArray[i] = value;  // create array index & val
    
      return this.arr;
    }
    
    YourArray.prototype.push = function( value ) {
      this.arr[ this.index++ ] = value;
      return this;
    };
    
    YourArray.prototype.pop = function( value ) {
      this.arr[ this.index-- ] = value;
      return this;
    };
    
    var arr = new YourArray();
    arr.push(2);
    console.log(mainArray); 
    
    var mainArray=[];//运行fn()后不再销毁数组
    函数数组(值){
    this.arr=mainArray;//查找全局元素|函数?
    该指数=0;
    var l=mainArray.length;
    if(this.arr===‘未定义’)
    mainArray+=value;//如果数组为空,则添加值
    其他的
    对于(var i=0;i
    我的假设是需要一个全局数组变量来存储 元素。是这样吗

    不,那是不对的

    您希望每个数组对象都有自己的独立数据集。否则,程序中怎么可能有多个数组

    function YourArray(value) {
      this.arr = [];  // This is the data belonging to this instance. 
      this.index = 0;
      if(typeof(value) != 'undefined')) {
        this.arr = [value];
        this.index = 1;
      }
    }
    
    ////////////////////////////////////
    // Add prototype methods here
    ///////////////////////////////////
    
    var array1 = new YourArray();
    var array2 = new YourArray();
    array1.push(2);
    array1.push(4);
    array2.push(3);
    array2.push(9);
    // Demonstrate that the values of one array
    // are unaffected by the values of a different array
    expect(array1.pop()).to.be(4);
    expect(array2.pop()).to.be(9);
    

    我承认,这次聚会有点晚了,但这让我很恼火。在一个全局数组中是否没有简单的方法(对于较大的“easy”值)

    标准阵列函数的工作原理如以下粗略(!)示意图所示:

    到目前为止,一切顺利。我们可以毫无问题地处理第一个数组。我们甚至可以制作第二个,但是当第一个想要扩展它的阵列时,我们就遇到了麻烦:没有空间了。第二个阵列的起点是第一个阵列的终点,没有任何间隙

    一个简单的解决方案是使用数组数组

    globalArray = [
                    ["first subarray"],
                    ["second subarray"],
                    ...
                  ];
    
    在这种情况下,我们不能重复使用我们已经编写的内容

    var globalArray = [];
    function YetAnotherArray(){
      // open a new array
      globalArray[globalArray.length] = [];
      // point to that array
      this.arr = globalArray[globalArray.length - 1];
      this.index = 0;
    }
    YetAnotherArray.prototype.push = function() {
      for(var i=0;i<arguments.length;i++){
        this.arr[ this.index++ ] = arguments[i];
      }
      return this;
    };
    // and so on
    
    但是,正如你可能已经猜到的那样:它不起作用

    YetAnotherArray.delete(yaa_3); // yaa_3 was 21,22,23,24,25,26,27,28,29
    globalArray.join("\n");
    /*
    1,2,3,4,5,6,7,8,9
    21,22,23,24,25,26,27,28,29
    
    */
    
    我们需要另一个阵列来保持所有位置。实际实现对于读者来说是一个练习,但是如果你想实现一个类似JavaScript的数组,也就是说,对于任意内容,你真的,真的,真的应该使用双链接列表。或者一棵树。可能是b+树


    哦,顺便说一句:是的,你可以很容易地用
    {key:value}
    对象来完成,但是那会把工作中的所有乐趣都挤出去,不是吗?;-)

    是的,程序中可以有多个数组,即使是全局数组。但你必须为它编写自己的管理。我为我的Bigint库做了一次,希望它能更快(不,不是),并使用了书中非常简单的K&R malloc算法。导致了很多白发。啊,还有,还上传到。请注意:这不是为胆小的人准备的;-)有没有办法推动多个元素?例如,push(2)然后push(4)返回[2,4]。据我所知,一旦函数完成执行,数组对象就会被销毁。如果按照建议将存储封装在对象中,那么除非对象本身被销毁,否则它不会被销毁。对于推送多个元素,如果您愿意,可以编写一个包含数组参数的推送。看起来您玩得很开心。您似乎正在使用javascript引擎创建一个新层来执行javascript引擎已经在执行的任务:-)在部分中是我,有一些乐趣,是的,但是最初的想法是由OP提出的。另外:只要
    TypedArrays
    的长度是固定的,并且缺少正常
    Array
    所具有的许多东西,就有理由尝试解决这些问题。我承认,将我的示例基于类型化数组会更合理,但如果没有充分的理由,这会使它们过于复杂。这里描述的大多数方法也普遍适用,并且不绑定到Javascript,但我希望这不会太违反这里的规则。
    var globalArray = [];
    function YetAnotherArray(){
      // open a new array
      globalArray[globalArray.length] = [];
      // point to that array
      this.arr = globalArray[globalArray.length - 1];
      this.index = 0;
    }
    YetAnotherArray.prototype.push = function() {
      for(var i=0;i<arguments.length;i++){
        this.arr[ this.index++ ] = arguments[i];
      }
      return this;
    };
    // and so on
    
    var globalArray = [];
    function YetAnotherArray(){
      // add a new subarray to the end of the global array
      globalArray[globalArray.length] = [];
      this.arr = globalArray[globalArray.length - 1];
      this.index = 0;
      this.pos = globalArray.length - 1;
    }
    YetAnotherArray.prototype.push = function() {
      for(var i=0;i<arguments.length;i++){
        this.arr[ this.index++ ] = arguments[i];
      }
      return this;
    };
    YetAnotherArray.prototype.toString = function() {
      var delimiter = arguments.length > 0 ? arguments[0] : ",";
      var output = "";
      for(var i=0;i<this.index;i++){
        output += this.arr[i];
        if(i < this.index - 1)
         output += delimiter;
      }
      return output;
    }
    // we need a method to delete an instance
    YetAnotherArray.prototype.clear = function() {
      globalArray[this.pos] = null;
      this.arr = null;
      this.index = null;
    };
    YetAnotherArray.delete = function(arr){
        arr.clear();
        delete(arr);
    };
    // probably won't work, just a hint in case of asynch. use
    var mutex = false;
    YetAnotherArray.gc = function() {
      var glen, indexof, next_index, sub_len;
    
      indexof = function(arr,start){
        for(var i = start;i<arr.length;i++){
          if (arr[i] == null || arr[i] == undefined)
            return i;
        }
        return -1;
      };
    
      mutex = true;
      glen = globalArray.length;
      sublen = 0;
      for(var i = 0;i<glen;i++){
        if(globalArray[i] == null || globalArray[i] == undefined){
          next_index = indexof(globalArray,i);
          if(next_index == -1){
            break;
          }
          else {
            globalArray[i] = globalArray[next_index + 1];
            globalArray[next_index + 1] = null;
            sublen++;
          }
        }
      }
      globalArray.length -= sublen - 1;
      mutex = false;
    };
    
    var yaa_1 = new YetAnotherArray();
    var yaa_2 = new YetAnotherArray();
    var yaa_3 = new YetAnotherArray();
    var yaa_4 = new YetAnotherArray();
    
    yaa_1.push(1,2,3,4,5,6,7,8,9).toString(); // 1,2,3,4,5,6,7,8,9
    yaa_2.push(11,12,13,14,15,16).toString(); // 11,12,13,14,15,16
    yaa_3.push(21,22,23,24,25,26,27,28,29).toString();// 21,22,23,24,25,26,27,28,29
    yaa_4.push(311,312,313,314,315,316).toString(); // 311,312,313,314,315,316
    
    
    globalArray.join("\n");
    /*
    1,2,3,4,5,6,7,8,9
    11,12,13,14,15,16
    21,22,23,24,25,26,27,28,29
    311,312,313,314,315,316
    */
    YetAnotherArray.delete(yaa_2);
    globalArray.join("\n");
    /*
    1,2,3,4,5,6,7,8,9
    
    21,22,23,24,25,26,27,28,29
    311,312,313,314,315,316
    */
    YetAnotherArray.gc();
    globalArray.join("\n");
    /*
    1,2,3,4,5,6,7,8,9
    21,22,23,24,25,26,27,28,29
    311,312,313,314,315,316
    */
    
    YetAnotherArray.delete(yaa_3); // yaa_3 was 21,22,23,24,25,26,27,28,29
    globalArray.join("\n");
    /*
    1,2,3,4,5,6,7,8,9
    21,22,23,24,25,26,27,28,29
    
    */