Javascript 4D arrays

前端 未结 8 537
一生所求
一生所求 2020-12-06 03:39

Anyone have a function to create 4D arrays (or any number of dimensions for that matter)?

I\'d like to call the function, then after that I can do something like

相关标签:
8条回答
  • 2020-12-06 04:24

    Very simple function, generate an array with any number of dimensions. Specify length of each dimension and the content which for me is '' usually

    function arrayGen(content,dims,dim1Len,dim2Len,dim3Len...) {
      var args = arguments;
      function loop(array,dim) {
        for (var a = 0; a < args[dim + 1]; a++) {
          if (dims > dim) {
            array[a] = loop([],dim + 1);
          } else if (dims == dim) {
            array[a] = content;
          }
        }
        return array;
      }
      var thisArray = [];
      thisArray = loop(thisArray,1);
      return thisArray;
    };
    

    I use this function very often, it saves a lot of time

    0 讨论(0)
  • 2020-12-06 04:26

    Quick and dirty:

    var arr = [[[[[]]]]];
    

    Check it out http://jsfiddle.net/MJg9Y/

    Note: You will still need to initialize each dimension. The above creates the foundation for a 4 dimension array at arr[0].

    0 讨论(0)
提交回复
热议问题