[英]How can I turn coordinates to two dimensional array in javascript
in my project I have to turn a bunch of coordinates to some meaningful two-dimensional array but I really don't know how to do it.在我的项目中,我必须将一堆坐标转换为一些有意义的二维数组,但我真的不知道该怎么做。 Can somebody help?有人可以帮忙吗?
To explain what I exactly want, let me give an example:为了解释我到底想要什么,让我举个例子:
Let's suppose that I have these 2 arrays(the reason that I started from one is because 0 and the last element of my rows are borders):假设我有这两个数组(我从一个开始的原因是因为 0 和我的行的最后一个元素是边框):
[[1, 1], [1, 2], [1, 4], [1, 5], [1, 8], [1, 9], [1, 10],[2, 1], [2, 2], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 10]]
Let the value inside these coordinates be like [row,col].让这些坐标内的值类似于 [row,col]。 And let's say I wan't to match them to generate some sort of two-dimensional array and each of the elements should contain the value '#'.假设我不想匹配它们来生成某种二维数组,并且每个元素都应该包含值“#”。 However, for example;但是,例如;
[1, 2], [1, 4]
[2, 2], [2, 4]
If there's a coordinate missing between two of these elements, they should be separated, meaning that there should be two different two-dimensional arrays, being split from that coordinate.如果其中两个元素之间缺少坐标,则应该将它们分开,这意味着应该有两个不同的二维 arrays,从该坐标中分割出来。 In this case, the result should be;在这种情况下,结果应该是;
// First two-dimensional array
const firstArray = [
['#','#'],
['#','#']
]
const secondArray = [
['#','#','','','#','#','#'],
['#','#','#','#','#','','#'],
]
In the second array, there are some '' values, but that is because the there are some coordinates missing(for [1, 5] and [1, 8], [1,6] and [1,7] are missing).在第二个数组中,有一些 '' 值,但那是因为缺少一些坐标(对于 [1, 5] 和 [1, 8],缺少 [1,6] 和 [1,7]) . So that should be considered too.所以这也应该考虑。
If you didn't understand please comment under the question me so that I can explain it to you.如果您不明白,请在我的问题下发表评论,以便我向您解释。
How can I come up with the functionality that I'm looking for?我怎样才能想出我正在寻找的功能?
You can accomplish both steps with a single Array#reduce()
call by using the coordinates themselves to place each [row, col]
in its relevant place in the matrix.您可以使用单个Array#reduce()
调用来完成这两个步骤,方法是使用坐标本身将每个[row, col]
放置在矩阵中的相关位置。
Here using an OR short circuit for assigning new sub-arrays, with a commented out replacement using the logical nullish assignment operator (??=) , and the comma operator for shorthand return in the arrow function .这里使用OR 短路来分配新的子数组,使用逻辑空赋值运算符 (??=)进行注释替换,并在箭头 function中使用逗号运算符进行速记返回。
const coords = [[1, 1], [1, 2], [1, 4], [1, 5], [1, 8], [1, 9], [1, 10], [2, 1], [2, 2], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 10]]; const matrix = coords.reduce((acc, [row, col]) => ( // using OR short circuit for compatibility (acc[row - 1] || (acc[row - 1] = []))[col - 1] = [row, col], acc // using logical nullish assignment operator (??=) //(acc[row - 1]??= [])[col - 1] = [row, col], _matrix ), []) // logging for (const row of matrix) { console.log(`[[${row.join('], [')}]]`) }
.as-console-wrapper { max-height: 100%;important: top; 0; }
const input = [[1, 1], [1, 2], [1, 4], [1, 5], [1, 8], [1, 9], [1, 10],[2, 1], [2, 2], [2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [2, 10]] const result = input.reduce((acc, [x, y]) => { acc[x - 1]??= [] const previousY = acc[x - 1][acc[x-1].length - 1]; if (previousY) { const delta = y - previousY; if (delta > 1) acc[x-1].push(...Array.from({length: delta - 1})); } acc[x-1].push(y); return acc }, []) console.log('1234567890') console.log( result.map(row => row.map(coor => coor? '#': ' ').join('') ).join('\n'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.