Skip to content

The Mdim library is created for multidimensional JavaScript arrays.

License

Notifications You must be signed in to change notification settings

samunemeth/mdim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JavaScript Mdim library

This library is made for multidimensional arrays in JavaScript. It is fully ES6 compatible. The minified version (the one with the extension .min.js) includes all the functions from the original library, it just removes the comments an the readability.


Importing the library:

You can either:


Standalone functions:


make2DArray(x, y)

This function creates a 2 dimensional array.

Example:

let grid = make2DArray(10, 10); // make grid a 10 by 10 array
console.log(grid); // this should log something like: [[10 x empty], [10 x empty], ...]

makeArray(dim)

This function creates an n dimensional array from an array that contains dimensions.

Example:

let grid = make2DArray([10, 3, 5]); // make grid a 10 by 3 by 5 array
console.log(grid); // this should log something like: [[[5 x empty], [5 x empty] ...] ...]


Array operations:


copy()

This function copies an array using the spread operator. It also works on multidimensional arrays.

Example:

let a = [0, 1, 2]; // make an array
let b = a.copy(); // copy the array

a[0] = 4; // change an element in the original array

console.log(a); // this should log something like: [4, 1, 2]
console.log(b); // this should log something like: [0, 1, 2]

forEachM( f(elem, pos) )

This is a multidimensional version of the forEach() function. It loops trough all the nested arrays. It takes a function as a parameter, and executes it for every element in the array. It gives the function the current element, and the position of the element.

Example:

let grid = make2DArray(10, 10); // make grid a 10 by 10 array

grid.forEachM((elem, pos) => {
    console.log(elem, ' at position ', pos); // logs every element and it's position to the console
});

fillM( val ) or fillM( f(pos, elem) )

This function fills a multidimensional array with something. It takes either a function, or a constant value. It gives the function the position of the current spot, and the element that will be replaced with the output of the function.

Example:

let grid = make2DArray(10, 10); // make grid a 10 by 10 array

grid.fillM(3); // fill the grid with the number 3
console.log(grid); // this should log something like: [[3, 3, 3, ...] ...]

grid.fillM((pos, elem) => {
    return {x: pos[0], y: pos[1], old: elem}; // fill the grid with object that contain the objects position and the old value (in this case: 3)
});
console.log(grid); // this should log something like: [[{x: 0, y: 0, old: 3}, ...] ...]

dim()

The dim() method returns the dimensions of a multidimensional array.

Example:

let grid = make2DArray(10, 5); // make grid a 10 by 5 array

console.log(grid.dim()); // should log: [10, 5]


To-do:

  • Add description to all functions.
  • Rework copy().
  • Remove removeElement().
  • Add rotate().
  • Add example projects

About

The Mdim library is created for multidimensional JavaScript arrays.

Topics

Resources

License

Stars

Watchers

Forks