在 JavaScript 中将 2D 数组的 2D 网格展平为单个 2D 数组(功能上?)
我有一个由 2D 数组(块)组成的 2D 数组(网格),用于我正在开发的游戏:
const c1 = [[1, 2],
[3, 4]]
const c2 = [[5, 6],
[7, 8]]
const c3 = [[9, 0],
[1, 2]]
const c4 = [[3, 4],
[5, 6]]
const grid_of_chunks = [[c1, c2],
[c3, c4]];
并且我想将 grid_of_chunks
减少/展平为:
[[1, 2, 5, 6],
[3, 4, 7, 8],
[9, 0, 3, 4],
[1, 2, 5, 6]]
我已经能够实现功能性解决方案为此(在 Clojure 的 2 行中),但我正在努力将其翻译为函数式 JavaScript,并弥合两种语言的 map
语义之间的差距(JS map只接受一个数组,而 Clojure 的 map
接受许多集合...)。
据我所知:
function join_grid_of_chunks(gofc) {
const joined_horiz = gofc.map(
gofc_row => [].map.apply(gofc_row, [cs => [].concat.apply(cs)])
);
return [].concat.apply(joined_horiz);
}
编辑:Clojure 解决方案(适用于任意大小的方形网格中均匀大小的方形块):
(defn join-grid-of-chunks [gofc]
(let [joined (map #(apply map concat %) gofc)]
(apply concat joined)))
I have a 2D array (grid) of 2D arrays (chunks) for a game I'm developing:
const c1 = [[1, 2],
[3, 4]]
const c2 = [[5, 6],
[7, 8]]
const c3 = [[9, 0],
[1, 2]]
const c4 = [[3, 4],
[5, 6]]
const grid_of_chunks = [[c1, c2],
[c3, c4]];
and I want to reduce/flatten the grid_of_chunks
to:
[[1, 2, 5, 6],
[3, 4, 7, 8],
[9, 0, 3, 4],
[1, 2, 5, 6]]
I've been able to implement a functional solution for this (in 2 lines of Clojure), but I'm struggling to wrap my head around translating it to functional JavaScript, and bridging the gap between the two language's map
semantics (JS map only accepts one array, whereas Clojure's map
accepts many collections...).
This is as far as I got:
function join_grid_of_chunks(gofc) {
const joined_horiz = gofc.map(
gofc_row => [].map.apply(gofc_row, [cs => [].concat.apply(cs)])
);
return [].concat.apply(joined_horiz);
}
Edit: Clojure solution (which works for uniformly-sized square chunks, in an arbitrarily sized square grid):
(defn join-grid-of-chunks [gofc]
(let [joined (map #(apply map concat %) gofc)]
(apply concat joined)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 flatMap 的更通用解决方案是映射每个网格行的第一个块的索引。
使用
zip
函数(例如 lodash 中的函数) ),你可以把它写得稍微优雅一些:zip
加上map
似乎很接近Clojure的带有多个集合的map
。这应该适用于二维网格中任何形状的二维块。A more general solution using
flatMap
is to map over the indices from the first chunk of each grid row.With a
zip
function (such as the one in lodash), you could write it slightly more elegantly as:zip
plusmap
seems to be close to Clojure'smap
with multiple collections. This should work for any shape 2d chunks in a 2d grid.这是我所拥有的:
应该适用于 NxN 块和 MxM 网格
Here's what I have:
Should work for NxN chunks and MxM grid