Include a built-in matrix / table / column primitive

// In the following example, 
// rows are allowed to grow dynamically;
// empties are filled with zeroes 

let tbl = Table( 4 ) // 0 0 0 0 //

tbl.push( 10 ) // 10 0 0 0 //
tbl.pop() // 0 0 0 0 //
tbl.unshift( 10 ) // 0 0 0 10 //
tbl.shift() // 0 0 0 0 //

tbl.push( 11 ) // 10 11 0 0 //
tbl.push( 12 ) // 10 11 12 0 //
tbl.push( 13 ) // 10 11 12 13 //
tbl.push( 14 ) // 10 11 12 13 / 14 0 0 0 //

tbl.row( 0 ) // [ 10, 11, 12, 13 ]
tbl.row( 1 ).push( 15, 16, 17, 18, 19, 20, 21, 22 ) // 10 11 12 13 / 14 15 16 17 / 18 19 20 21 / 22, 0, 0, 0 //
tbl.col( 0 ) // [10, 14, 18, 22] //
tbl.cols() // [ [10, 14, 18, 22], [11, 15, 19, 0], [12, 16, 20, 0 ], [13, 17, 21, 0] ] //
tbl.diag() // [ [ 10, 15, 20, 0], [13, 16, 19, 22 ] ] //
tbl.utriag( 0 ) // ([ 10 11 12 13 / 14 15 16 0 / 18 19 0 0 / 22 0 0 0 ], FALSE) //
tbl.ltriag( 0 ) // ([ 10 0 0 0 / 14 15 0 0 / 18 19 20 0 / 22 0 0 0 ], FALSE) //
tbl.rot( 0 ) // 13 17 21 0 / 12 16 20 0 / 11 15 19 0 / 10, 14, 18, 22 //
tbl.hemi( 0 ) // [ [ 10, 11, 12, 13 ], [ 14, 15, 16, 17 ] ] //


let tbl_2 = Table( 4 ) // 0 0 0 0 //

tbl_2.push( 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 ) // 10 11 12 13 / 14 15 16 17 / 18 19 20 21 / 22, 0, 0, 0 //

let result = tbl + tbl_2; // 20 22 24 26 / 28 30 32 34 / 36 38 40 42 / 44, 0, 0, 0 //

let tbl_3 = Table( 4 ).push( 'abcd'.split() ) // a b c d //

let result_2 = tbl_3 + Table( 4 ).push( 'bbbb'.split() ) // ab bb cb db //

This seems like a very niche, and very confusing, usage for 2D arrays. I think specific use-cases should just define such a class on the fly instead of adding it to native JS.

At the very least, make an npm package and wait til it’s heavily used, or until huge drawbacks are revealed that a native implementation would alleviate, so it’s a more convincing argument.

I dunno, I could see use cases for this sort of thing.

Though, perhaps it doesn't need to be its own, unique data structure. It would be nice to have static array methods that were capable of operating on 2d arrays. I've certainly needed things like that.

Array.diagnol([
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
]) // [1, 5, 9]

// etc...

See my suggestion here: Provide an easy way to create a new array, filled via a mapping function - #37 by graphemecluster.
Btw, Array.prototype.nestedForEach and Array.prototype.nestedMap are good ideas too:

[5, 7]
  .buildShape((i, j) => i * 7 + j + 1)
  .nestedMap((point, [i, j]) => `Point #${point} is at (${i}, ${j})`)
  .nestedForEach(message => console.log(message));
/* logs
Point #1 is at (0, 0)
Point #2 is at (0, 1)
……
Point #35 is at (4, 6)
*/

We definitely need more built-in methods from lodash and Python numpy :)

I’ve just made a package doing this, see Provide an easy way to create a new array, filled via a mapping function - #43 by graphemecluster.

Started a new post: Proposal for Array.prototype methods for multi-dimensional arrays