How can I turn coordinates to two dimensional array in javascript

Viewed 225

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):

[[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]. 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. 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). 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?

2 Answers

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.

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.

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'))

Related