分割数组

发布于 2024-11-06 22:41:30 字数 243 浏览 1 评论 0原文

我有一个逗号分隔值数组,例如 var myArray = [0,1,0,1,0,0,0,0,1]

我想根据计数拆分它(如果 count 为) 3 我想最终得到三个数组,就像

var myArrayA = [0,1,0];
var myArrayB = [1,0,0];
var myArrayC = [0,0,1];  

我需要将其视为 3x3 矩阵并进行转置。

I have an array of comma separated values like var myArray = [0,1,0,1,0,0,0,0,1]

I want to split it based on a count if count is 3 I want to end up with three arrays like

var myArrayA = [0,1,0];
var myArrayB = [1,0,0];
var myArrayC = [0,0,1];  

I need to treat it as a 3x3 matrix and do a transpose.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弃爱 2024-11-13 22:41:30

array.slice 我的建议是

根据您的具体情况有一个看看这个

<script>
function getMatrix(arr,count) {
  var res = {};
  for (var i=0;i<arr.length;i+=count) {
    res[i]=arr.slice(i,(i+count));
  }
  return res;
}
var result = getMatrix("0,1,0,1,0,0,0,0,1".split(","),3);
for (var o in result) alert(result[o]);
</script>

array.slice would be my suggestion

in your SPECIFIC situation have a look at this

<script>
function getMatrix(arr,count) {
  var res = {};
  for (var i=0;i<arr.length;i+=count) {
    res[i]=arr.slice(i,(i+count));
  }
  return res;
}
var result = getMatrix("0,1,0,1,0,0,0,0,1".split(","),3);
for (var o in result) alert(result[o]);
</script>
那伤。 2024-11-13 22:41:30

尝试 array.slice(start, end)

示例:-

var firstArray = array.slice(0,count);
var secondArray = array.slice(count);

Try array.slice(start, end)

Example:-

var firstArray = array.slice(0,count);
var secondArray = array.slice(count);
未央 2024-11-13 22:41:30

乐趣!
您真正需要知道的唯一一件事是,要转置,请为矩阵中的所有 (i,j) 设置 m(i,j) = m(j,i)。我最终转换为矩阵表示,它不太简洁但更易读......

var a = [1,2,3,4,5,6];
function listToSquareMatrix(a,colDim) {
  a = a.slice(0);
  var res = [];
  var row = 0;
  var col;
  for(; a.length; row++) {
    for(col = 0; col < colDim; col++) {
      res.push({
        row : row,
        col : col,
        val : a.shift()
      });
    }
  }
  return res;
}
function transpose(m) {
  for(var i=0; i < m.length; i++) {
    var v = m[i];
    var row = v.row;
    v.row = v.col;
    v.col = row;
  }
  return m.sort(function (a,b) { var r = compare(a.row,b.row); var c = compare(a.col,b.col); return r !== 0 ? r : c });
}
function compare(a,b) {
  if (a < b) {
    return -1;
  } else if (b < a) {
    return 1;
  }
  return 0;
}
function matrixToArray(m) {
  var res = [];
  for (var i = 0; i < m.length; i++) {
    res.push(m[i].val);
  }
  return res;
}
console.log(listToSquareMatrix(a, 3));
console.log(matrixToArray(transpose(listToSquareMatrix(a, 3))));
console.log(matrixToArray(transpose(transpose(listToSquareMatrix(a, 3)))));

Fun!
The only thing you need to know really is that to transpose, set m(i,j) = m(j,i) forall (i,j) in the matrix. I ended up converting to a matrix representation, which is less concise but mor legible...

var a = [1,2,3,4,5,6];
function listToSquareMatrix(a,colDim) {
  a = a.slice(0);
  var res = [];
  var row = 0;
  var col;
  for(; a.length; row++) {
    for(col = 0; col < colDim; col++) {
      res.push({
        row : row,
        col : col,
        val : a.shift()
      });
    }
  }
  return res;
}
function transpose(m) {
  for(var i=0; i < m.length; i++) {
    var v = m[i];
    var row = v.row;
    v.row = v.col;
    v.col = row;
  }
  return m.sort(function (a,b) { var r = compare(a.row,b.row); var c = compare(a.col,b.col); return r !== 0 ? r : c });
}
function compare(a,b) {
  if (a < b) {
    return -1;
  } else if (b < a) {
    return 1;
  }
  return 0;
}
function matrixToArray(m) {
  var res = [];
  for (var i = 0; i < m.length; i++) {
    res.push(m[i].val);
  }
  return res;
}
console.log(listToSquareMatrix(a, 3));
console.log(matrixToArray(transpose(listToSquareMatrix(a, 3))));
console.log(matrixToArray(transpose(transpose(listToSquareMatrix(a, 3)))));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文