Javascript 表到矩阵(解决行跨/列跨)

问题描述 投票:0回答:1
javascript
1个回答
0
投票

你的算法有一个错误,但它确实有效。

我不完全理解你的代码,但如果我不得不猜测, 条件

info.x == x
期望每次“跳跃”时
x
都会递增 找到(在本例中已过滤)。 所以你不能只将这个
jumpAmounts
偏移量存储在变量中 稍后将其添加到
x

我完全删除了过滤功能,并将其替换为 for 循环,其中

x
“当场”递增:

// old code
const jumpAmounts = jumps.filter(info => info.y == y && info.x == x);

if (jumpAmounts && jumpAmounts.length > 0)
    x += jumpAmounts.length;

新代码:

for (let info of jumps)
    if (info.y === y && info.x === x)
        x++;

jsfiddle

© www.soinside.com 2019 - 2024. All rights reserved.