Creates an empty Array2D with fixed dimensions and an optional initial value for each element.
// create an array with width = 3 and height = 6
Array2D.create(3, 6)
// create an array with width = 2, height = 2, filled with "a"
Array2D.create(2, 2, "a")
The type of the elements in the array
The new array
Creates an Array2D from a two dimensional array.
Each element in array
corresponds to a row in the returned array.
The height
will be the number or rows and the width
will be the
length of the longest row.
let map = [
[0, 1, 0],
[1, 1, 1],
[0, 0, 0],
];
Array2D.from2DArray(map)
If you want to create an Array2D from a one dimensional (flat) array, then use fromArray instead.
The type of the elements in array
The two-dimensional array
Creates an Array2D from a one dimensional array in row-major order.
let map = [
0, 1, 0,
1, 1, 1,
0, 0, 0,
];
Array2D.fromArray(map, 3, 3);
The type of the elements in the array
The width of the array in columns
The height of the array in rows
The one dimensional array. The length of array
should be equal to width * height
The new array
Creates a two dimensional array from array2D
.
let map = Array2D.create(2, 2, 0);
Array2D.to2DArray(map) // [[0, 0], [0, 0]]
The type of the elements in array2D
The two dimensional array
Creates a one dimensional array in row-major order. from an Array2D.
let map = Array2D.create(2, 2, 0);
Array2D.toArray(map) // [0, 0, 0, 0]
The type of the elements in the array
The new one dimensional array
Creates a new Array2D by shallowly cloning an existing one.
let a1 = Array2D.create(10, 10);
// create a copy of an existing Array2D
let a2 = Array2D.clone(a1);
The type of the elements in the array
The array to clone
The cloned array
Performs a shallow comparison to determine whether a1
and a2
are
equivalent.
let a1 = Array2D.create(2, 2, 0);
let a2 = Array2D.create(2, 2, 0);
Array2D.equals(a1, a2) // true
Array2D objects are considered to be equivalent if:
The type of the elements in a1
and a2
Fills the elements of array2D
with value
.
Can optionally fill a subrectangle within the Array2D by passing start and end coordinates.
let a = Array2D.create(3, 3, "a");
// fill with "b"
Array2D.fill(a, "b");
// fill the top left quarter with "c"
Array2D.fill(a, "c", 0, 0, 2, 2);
This function modifies array2D
. Use filled to create a new
Array2D instead.
The type of the elements in array2D
The array to fill
The value to fill array2D
with
The x coordinate to start filling from
The y coordinate to start filling from
The x coordinate to finish filling before
The y coordinate to finish filling before
Creates a new Array2D by filling a copy of array2D
with value
.
Can optionally fill a subrectangle within the Array2D by passing the start and end coordinates.
let a = Array2D.create(3, 3, "a");
// fill with "b"
let b = Array2D.fill(a, "b");
// fill the top left quarter with "c"
let c = Array2D.fill(a, "c", 0, 0, 2, 2);
This function creates a new Array2D. Use fill to modify the existing one instead.
The element type of array2D
The array to fill
The value to fill array2D
with
The x coordinate to start filling from
The y coordinate to start filling from
The x coordinate to finish filling before
The y coordinate to finish filling before
The filled array2D
Gets the element at the given indices.
let a = Array2D.create(2, 2, 0);
// get the element at 0, 0
Array2D.get(a, 0, 0)
The type of the elements in array2D
The x index
The y index
The element (or undefined if the indices were out of bounds).
Determines whether value
is an Array2D.
An on optional type guard that will check the type of the elements of in the array.
// Check that grid is an Array2D<Point.Point>
Array2D.is(grid, Point.is)
function isNumber(value): value is number {
return typeof value === "number";
}
// Check that samples is an Array2D<number>
Array2D.is(samples, isNumber)
Maps over array2D
with a function to create a new Array2D.
This works like Array.prototype.map
but rather than having a single index passed into the callback, both
x and y coordinates are provided.
let a = Array2D.from2DArray([
[0, 1, 2],
[3, 4, 5]
]);
// Create a new Array2D by incrementing the value of each element
let b = Array2D.map(a, n => n + 1);
The element type of array2D
The element type of the returned Array2D
A function that takes a value of type T
and returns a value of type U
Sets the element at the given indices.
let a = Array2D.create(2, 2);
// set the element at 0, 0 to "a"
Array2D.set(a, 0, 0, "a")
The type of the elements in array2D
The x index
The y index
The value to insert
Creates an Array2D from rectangular slice of array2d
.
let a1 = Array2D.create(10, 10, "a");
// Copy the top left slice into a new Array2D
let a2 = Array2D.slice(a1, 0, 0, 5, 5);
The element type of array2D
The top left x coordinate of the slice
The top left y coordinate of the slice
The width of the slice
The height of the slice
The sliced rectangle as an Array2D
Generated using TypeDoc
Creates an empty Array2D with fixed dimensions and an optional initial value for each element.
// create an array with width = 3 and height = 6 Array2D.create(3, 6) // create an array with width = 2, height = 2, filled with "a" Array2D.create(2, 2, "a")