【问题标题】:Javascript: Updating a Matrix ArrayJavascript:更新矩阵数组
【发布时间】:2015-11-06 11:10:26
【问题描述】:

我需要更新矩阵数组的帮助。它从设定值开始,我需要传入坐标来更新值。

我有一个基础矩阵:

var myMatrix = [
    ['-','-','-','-','-','-','-','-','-','-'],
    ['-','-','-','-','-','-','-','-','-','-'],
    ['-','-','-','-','-','-','-','-','-','-'],
    ['-','-','-','-','-','-','-','-','-','-'],
    ['-','-','-','-','-','-','-','-','-','-'],
    ['-','-','-','-','-','-','-','-','-','-'],
    ['-','-','-','-','-','-','-','-','-','-'],
    ['-','-','-','-','-','-','-','-','-','-'],
    ['-','-','-','-','-','-','-','-','-','-'],
    ['-','-','-','-','-','-','-','-','-','-']
];

我能够将矩阵中的特定点更新为如下所示:

----x-----
-----x----
--------x-
--------x-
---x------
----------
-------x--
----------
----x-----
-------x--

使用此功能:

function stepTwo(coordinates) {
    console.log('Step Two');
    for(var j =0; j < coordinates.length; j ++) {
        myMatrix[coordinates[j].y][coordinates[j].x] = 'x';
    }
    for(var i = 0; i < myMatrix.length; i++) {
        console.log(myMatrix[i].join(' '));
    }
}

var coordinatesArray = [
    {x: 4, y: 0},
    {x: 5, y: 1},
    {x: 8, y: 2},
    {x: 8, y: 3},
    {x: 3, y: 4},
    {x: 7, y: 6},
    {x: 4, y: 8},
    {x: 7, y: 9},
];

stepTwo(coordinatesArray);

现在我想做另一个类似的函数,它应该像这样更新矩阵:

xxxxx-----
xxxxxx----
xxxxxxxxx-
xxxxxxxxx-
xxxx------
----------
xxxxxxxx--
----------
xxxxx-----
xxxxxxxx--

基本上传入行和多少个'-'转换成'x'。

我当前工作的 JS Fiddle(第三步是我需要帮助的地方): https://jsfiddle.net/2vbd27f0/73/

非常感谢您的帮助!

【问题讨论】:

  • 提示:使用Array.prototype.someArray.prototype.map。这个功能很容易。看看你的小提琴:jsfiddle.net/2vbd27f0/74
  • 请注意... stepTwo 没有更新数组。您将不得不返回结果。数组不是通过引用传递的。
  • @EmmanuelDelay First Google result。而且他甚至没有在任何地方传递数组。它是在全局范围内定义的,并且函数正在访问完全相同的变量(在范围内)。

标签: javascript arrays matrix


【解决方案1】:

这是我对这个问题的看法 - 通过所需的数字创建一个新的 x 数组,并连接切片的剩余部分 (fiddle):

function stepThree(points) {
    points.forEach(function(entry) {
        var row = myMatrix[entry.y];

        if(!row) { // if no row continue to next iteration
            return;
        }

        var fillNum = entry.x > row.length ? row.length : entry.x; // if the number to fill is higher than length use length

        myMatrix[entry.y] = new Array(fillNum + 1) // create an array which is bigger by 1 from places to fill
            .join('x') // join it using x's getting the right number
            .split('') // split it to create new array
            .concat(row.slice(fillNum)); // add the leftovers
    });
}

【讨论】:

    【解决方案2】:

    短解决方案

    我假设你想去points[j].y行并创建points[j].xx's。

    for(var j = 0; j < points.length; j++) {
        for(var k = 0; k < points[j].x; k++) {
            myMatrix[points[j].y][k] = 'x';
        }
    }
    

    工作原理

    外部for 循环遍历这些点。使用j 作为计数器,points[j] 是当前点。 points[j].y 是当前行号。

    对于这一行,我们要创建 point[j].x x's。所以有一个内部的for 循环从0 运行到point[j].x,使用变量k 作为它的计数器。

    然后在我们使用坐标(j, k) 选择的每个单元格中,我们将值设置为x

    更清晰的解决方案

    考虑到所有这些,我们可以使上述解决方案更易于理解。

    for(var pointNumber = 0; pointNumber < points.length; pointNumber++) {
        // Pick one point
        var currentPoint = points[pointNumber];
    
        // We want to set the points (0, row) .. (currentPoint.x, row), so we use another loop.
        var row = currentPoint.y;
        // Set all the columns to 'x'
        for(var column = 0; column < currentPoint.x; column++) {
            myMatrix[row][column] = 'x';
        }
    }
    

    【讨论】:

    • 您可以优化您的 for 循环或更好:使用 forEach
    • @Shadowen 正是我需要的,谢谢!
    • @JoshuaK forEach is less efficient than for。无论如何,重点不是优化;这是学习。如果你想优化,你首先要从points 中删除所有重复的行...
    • @Shadowen optimize 是错误的词。我要改进。 forEach 是比使用 for 更好的编码风格(如果你想遍历数组)。在看到代码的第一刻,你在做什么就很清楚了。 for(...) 你必须“分析”这个表达式。速度……是的。有时这很糟糕,但在这种情况下,我们没有高性能应用程序。如果您使用 for 而不是 foreach,则使用 for(var i=0,pointNumber=points.length;i&lt;n;i++) 避免在循环的每个步骤中处理长度属性。只是一个建议:)
    • @JoshuaK 这点已经讨论过很多次了。 Pleasereadthesethreads。使用简单的for 循环通常是最简单的(风格方面)。再次,我必须向您指出 JSPerf。如果您使用的是现代浏览器(Chrome、IE9 等),我相信 for 实际上比 for cached 更快。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多