【问题标题】:Changing rows to columns javascript将行更改为列javascript
【发布时间】:2013-10-18 23:28:25
【问题描述】:

我想将行更改为数组的列。

[
  [1],
  [1,2],
  [1,2,3],
  [4,2,3],
  [4,5,3],
  [4,5,6]
]

到 [ [1,1,1,4,4,4], [2,2,2,5,5], [3,3,3,6] ]

我试过了

var res = [];

    for(i in this.fields) {

        for(j in this.fields[i].value) {

            if(i === 0) res[j] = [];
            res[j][i] = this.fields[i].value[j];

        }

    }

这给了我一个空集。

【问题讨论】:

标签: javascript jquery


【解决方案1】:

你的问题看起来有点奇怪,因为你有不同的长度并且你忽略了 undefined 值,但它仍然可以实现。

数组不要使用for..in循环,使用普通的for。此外,您需要知道您的新父 Array 中有多少项,这是原始子 Arrays 长度的 max .

var arrR = [ // will refer to "down" and "across" as in this literal
        [1],
        [1, 2],
        [1, 2, 3],
        [4, 2, 3],
        [4, 5, 3],
        [4, 5, 6]
    ];
function r2c(arr) {
    var arrC = [], // next get the longest sub-array length
        x = Math.max.apply(Math, arr.map(function (e) {return e.length;})),
        y = arr.length,
        i, j;
    for (i = 0; i < x; ++i) {   // this is the loop "down"
        arrC[i] = [];
        for (j = 0; j < y; ++j) // and this is the loop "across"
            if (i in arr[j])
                arrC[i].push(arr[j][i]);
    }
    return arrC;
}
var arrC = r2c(arrR);
/* [
    [1, 1, 1, 4, 4, 4],
    [2, 2, 2, 5, 5],
    [3, 3, 3, 6]
] */

您仍然应该考虑是否对[[1], [1, 2], [1]] 变成[[1, 1, 1], [2]] 感到满意,我认为这是出乎意料的(2 的位置完全丢失了),但似乎正是您想要的。

【讨论】:

    【解决方案2】:

    与 Pauls 类似,但不需要先获得最大长度:

    function transpose(arr) {
    
      // Loop over arrays as long as one has values
      // Arrays should be contiguous, may fail if sparse
      for (var result = [], i=0, more; more; i++) {
        more = false;
    
        // Get the ith element of each array (if there is one)
        for (var j=0, jLen=arr.length; j<jLen; j++) {
    
          // Don't add missing members
          if (arr[j].hasOwnProperty(i)) {
    
            // Add array for result if not already there
            result[i] = result[i] || [];
    
            // Do transpose
            result[i][j] = arr[j][i];
    
            // Only keep going while there is data
            more = true;
          }
        }
      }
      return result;
    }
    

    顺便说一句,您原始功能的固定版本是:

    function transpose2(fields) {
        // Make sure the result array is initialised
        var res = [];
    
        // Don't forget to keep counters local - declare them
        // I've removed *this* as it's a plain function, use it if 
        // it's an instance method
        for(var i in fields) {
    
            // Values are read directly, there is no "value" accessor
            for(var j in fields[i]) {
    
                // Don't rely on order of enumeration - may not start at 0
                if(!res[j]) res[j] = [];
    
                // Do the transpose
                res[j][i] = fields[i][j];
            }
        }
        return res;
    }
    

    但如上所述,数组不喜欢 for..in,特别是因为有许多库扩展了像 Array.prototype 这样的内置函数,因此您也将遍历这些属性。但如果你对此很满意,这是处理稀疏数组的好方法。您可以添加 hasOwnProperty 测试以避免继承枚举。

    还要注意,枚举的顺序不一定是从 '0' 或任何特定的顺序,因此改变了初始化 res[j] 的方式。

    【讨论】:

      【解决方案3】:

      创建这个函数:

      function transpose(arr) {
              return Object.keys(arr[0]).map(function (c) {
                  return arr.map(function (r) {
                      return r[c];
                  });
              });
          }
      

      然后:

      var transposedArray = transpose(originalArray);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-14
        • 1970-01-01
        • 1970-01-01
        • 2022-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多