-1

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

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

        }

    }

这给了我空集。

4

3 回答 3

3

创建这个函数:

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

接着:

var transposedArray = transpose(originalArray);

于 2014-09-17T10:02:05.237 回答
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;
}

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

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]

于 2013-10-11T02:36:11.217 回答
1

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

不要对Arrayfor..in使用循环,使用普通的. 此外,您需要知道新的父Array中有多少项目,这是原始子Arrays长度的最大值for

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完全失去了的位置),但似乎是您想要的。

于 2013-10-11T01:23:49.017 回答