简体   繁体   English

如何在 javascript 中将坐标转换为二维数组

[英]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.

相关问题 如何在javaScript中创建一个带有索引和键的二维数组? - How can I create a two dimensional array with index and with key in javaScript? 如何在 JavaScript 中创建一个二维数组? - How can I create a two dimensional array in JavaScript? 我怎样才能在javascript中对3维数组进行排序? - How can i sort 3 dimensional array in javascript? 如何在JavaScript中将带有JSON对象的二维数组转换为单个数组? - How can I make two dimensional array with JSON objects into single array in javascript? 如何将一维数组变成二维数组,使父母可以深入两个以上的层次? - How to turn a one dimensional array into a two dimensional one where the parents can go more than two levels deep? 如何将带索引的二维数组从 PHP 返回到 JavaScript - How can I return indexed two-dimensional array from PHP to JavaScript 如何对 JavaScript 中的二维数组进行排序? - How do I sort a two dimensional array in JavaScript? 当我在javascript中有一个对象时如何创建二维数组? - How to create two dimensional array when I have an object in javascript? 如何获取由二维数组(JS)制成的网格的坐标? - How to obtain coordinates of a grid made from a two dimensional Array (JS)? 如何获得这种格式的二维数组? - how can i get two dimensional array in this format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM