将行更改为列javascript

时间:2013-10-11 01:12:39

标签: javascript jquery

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

[
  [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];

        }

    }

这给了我空集。

3 个答案:

答案 0 :(得分:1)

你所问的内容看起来有点奇怪,因为你有不同的长度而你忽略了未定义的值,但它仍然是可以实现的。

不要对数组使用for..in循环,请使用普通for。此外,您需要知道新父数组中有多少项,这是原始子数组长度的 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的位置完全丢失了),但似乎是你的意思打算。

答案 1 :(得分:1)

与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;
}
BTW,原始功能的固定版本是:

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]的方式。

答案 2 :(得分:1)

创建此功能:

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

然后:

var transposedArray = transpose(originalArray);