如何在JavaScript中对角遍历数组 [英] How do I traverse an array diagonally in javascript

查看:43
本文介绍了如何在JavaScript中对角遍历数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,其中包含我想对角遍历的字符串.
假设:

I have an array with strings that I would like to traverse diagonally.
Assumptions:

  • 每个字符串的长度相同.
  • 数组可以是水平或垂直的正方形或矩形.

矩阵如下:

A B C D
E F G H
I J K L

我想得到(从左上方到右下方):

I Would like to get (from top left to bottom right):

A
EB
IFC
JGD
KH
L

和(从左下方到右上方):

and (from the bottom left to top right):

I
JE
KFA
LGB
HC
D

我已经有一段代码可以以3/4的方式工作,但是我似乎无法弄清楚自己在做什么(错误).

I already have a piece of code that works 3/4 of the way, but i cant seem to figure out what I am doing (wrong).

//the array
var TheArray = ['ABCD','EFGH','IJKL'];

//amount of rows
var RowLength = TheArray.length;
//amount of colums
var ColumnLength = TheArray[0].length;

我编写的代码将对角线切成这些循环中的4个,以获取所有对角线.对于for循环,它看起来像2,如果不循环未绑定的值.伪代码看起来像这样:

The code I have chops up the diagonals into 4 of these loops to get all the diagonals. It looks as 2 for loops with an if to not loop over unbound values. The pseudo code looks a bit like this:

for(loop rows){
 var outputarray = [];
   for(loop columns){
      if(delimit for out of bound){
       var temprow = TheArray[something?];
       var tempvalue = temprow[something?];
       outputarray.push(tempvalue);
       }
   }
 //use values
document.getElementById("theDiv").innerHTML += outputarray.join("")+"<br>";
}

我希望有人可以帮我这个忙.

I hope somebody can help me with this.

推荐答案

从左上方到右下方

var array = ["ABCD","EFGH","IJKL"];

var Ylength = array.length;
var Xlength = array[0].length;
var maxLength = Math.max(Xlength, Ylength);
var temp;
for (var k = 0; k <= 2 * (maxLength - 1); ++k) {
    temp = [];
    for (var y = Ylength - 1; y >= 0; --y) {
        var x = k - y;
        if (x >= 0 && x < Xlength) {
            temp.push(array[y][x]);
        }
    }
    if(temp.length > 0) {
        document.body.innerHTML += temp.join('') + '<br>';
    }
}

(另请参见 此小提琴 )

(see also this Fiddle)

var array = ["ABCD","EFGH","IJKL"];

var Ylength = array.length;
var Xlength = array[0].length;
var maxLength = Math.max(Xlength, Ylength);
var temp;
for (var k = 0; k <= 2 * (maxLength - 1); ++k) {
    temp = [];
    for (var y = Ylength - 1; y >= 0; --y) {
        var x = k - (Ylength - y);
        if (x >= 0 && x < Xlength) {
            temp.push(array[y][x]);
        }
    }
    if(temp.length > 0) {
        document.body.innerHTML += temp.join('') + '<br>';
    }
}

(另请参见 此小提琴 )

(see also this Fiddle)

由于两者之间只有一条线的区别,因此您可以轻松地将它们组合到一个函数中:

As there's but a single line of difference between both, you can easily combine them in a single function :

var array = ["ABCD","EFGH","IJKL"];

function diagonal(array, bottomToTop) {
    var Ylength = array.length;
    var Xlength = array[0].length;
    var maxLength = Math.max(Xlength, Ylength);
    var temp;
    var returnArray = [];
    for (var k = 0; k <= 2 * (maxLength - 1); ++k) {
        temp = [];
        for (var y = Ylength - 1; y >= 0; --y) {
            var x = k - (bottomToTop ? Ylength - y : y);
            if (x >= 0 && x < Xlength) {
                temp.push(array[y][x]);
            }
        }
        if(temp.length > 0) {
            returnArray.push(temp.join(''));
        }
    }
    return returnArray;
}

document.body.innerHTML = diagonal(array).join('<br>') +
                          '<br><br><br>' +
                          diagonal(array, true).join('<br>');

(另请参见 此小提琴 )

(see also this Fiddle)

这篇关于如何在JavaScript中对角遍历数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆