arraymethods.js
Version:
Useful array methods for 1d, 2d, and 3d arrays!
1,538 lines (1,330 loc) • 36.7 kB
Markdown
# ArrayMethods.js
This npm package includes array methods that make working with 1, 2, and 3 dimensional arrays much easier! I hope you find it useful :)
## Table of Contents
- [Installation](#installation)
- [Using the package](#using-the-package)
- [Methods for any array](#methods-for-any-array)
- [Exclusive 1d methods](#exclusive-1d-methods)
- [Exclusive 2d+ methods](#exclusive-2d-methods)
- [Exclusive 3d+ methods](#exclusive-3d-methods)
- [Coordinates Class](#coordinates-class)
- [ItemConditional Class](#itemconditional-class)
- [Extras](#extras)
## Installation
To install the `arrayMethods.js` package, simply do:
```
npm install arraymethods.js
```
## Using the Package
Using the package is simple and easy!
```js
const Arrays = require("arraymethods.js");
// or
import Arrays from "arraymethods.js";
```
All methods and constructors can be found from the `Arrays` package.
## Methods For Any Array
These methods can be used on ANY array, no matter the dimension!
### Table of Contents
- [GetDimension](#getdimension) - Get the dimension of an array
- [Swap](#swap) - Swap 2 items
- [Replace](#replace) - Replace items
- [InsertLeft](#insertleft) - Insert an item left of an another item
- [InsertRight](#insertRight) - Insert an item right of an another item
- [SearchQuery](#searchQuery) - Get all the items that match one or more conditions
- [LengthWith](#lengthWith) - Like searchQuery, but it returns the length with one or more CheckConditions
### GetDimension
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.getDimension(array));
// Returns: 1
```
```js
const array = [["apple", "orange"], ["lemon", "pear"]];
console.log(Arrays.getDimension(array));
// Returns: 2
```
```js
const array = [
[["apple", "orange"], ["lemon", "pear"]],
[["banana", "avocado"], ["peach", "grape"]]
]
console.log(Arrays.getDimension(array));
// Returns: 3
```
The `getDimension` method returns a number depending on the dimension of the array. Note that any arrays with more dimensions than 3 will return 3, since 4d+ arrays are not supported yet.
`Parameters:`
1) 1d+ array
`Returns:` number
### Swap
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.swap(array, "apple", "lemon"));
// Returns: ["lemon", "orange", "apple", "pear"]
```
```js
const array = [["apple", "orange"], ["lemon", "pear"]];
console.log(Arrays.swap(array, {row: 0, column: 0}, {row: 1, column: 0}));
// Returns: [["lemon", "orange"], ["apple", "pear"]]
```
```js
const array = [["apple", "orange"], ["lemon", "pear"]];
const coordinates1 = new Arrays.Coordinates(0, 0);
const coordinates2 = new Arrays.Coordinates(1, 0);
console.log(Arrays.swap(array, coordinates1, coordinates2));
// Returns: [["lemon", "orange"], ["apple", "pear"]]
```
The `swap` method returns a same-dimensional array, with two specified items swapped.
`Parameters:`
1) 1d+ array
2) Item, index, [array][1], [object][2], or [Coordinates][3]
3) Item, index, [array][1], [object][2], or [Coordinates][3]
`Returns:` SameD array
### Replace
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.replace(array, "apple", "lemon"));
// Returns: ["lemon", "orange", "lemon", "pear"]
```
```js
const array = [["apple", "orange"], ["lemon", "pear"]];
console.log(Arrays.replace(array, {row: 1, column: 0}, "lemon"));
// Returns: [["lemon", "orange"], ["lemon", "pear"]]
```
```js
const array = [["apple", "orange"], ["lemon", "pear"]];
const coordinates = new Arrays.Coordinates(1, 0);
console.log(Arrays.swap(array, coordinates, "lemon"));
// Returns: [["lemon", "orange"], ["lemon", "pear"]]
```
The `replace` method returns a same-dimensional array, with one or more items replaced.
`Parameters:`
1) 1d+ array
2) Item, index, [array][1], [object][2], or [Coordinates][3]
3) Item
`Returns:` SameD array
### InsertLeft
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.insertLeft(array, "orange", "mango"));
// Returns: ["apple", "mango", "orange", "lemon", "pear"]
```
```js
const array = [["apple", "orange"], ["lemon", "pear"]];
console.log(Arrays.insertLeft(array, {row: 0, column: 1}, "mango"));
// Returns: [["apple", "mango", "orange"], ["lemon", "pear"]]
```
```js
const array = [["apple", "orange"], ["lemon", "pear"]];
const coordinates = new Arrays.Coordinates(0, 1);
console.log(Arrays.insertLeft(array, coordinates, "mango"));
// Returns: [["apple", "mango", "orange"], ["lemon", "pear"]]
```
The `insertLeft` method returns a same-dimensional array, with one or more items inserted to the left of items specified.
`Settings:`
```js
{
insertAll: false, // inserts the item to the left of all items that match
replace: false // inserts the item in the place of the item to the left
}
```
Insert an object like this at the end of the method to choose your settings.
`Parameters:`
1) 1d+ array
2) Item, index, [array][1], [object][2], or [Coordinates][3]
3) Item
`Returns:` SameD array
### InsertRight
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.insertRight(array, "apple", "mango"));
// Returns: ["apple", "mango", "orange", "lemon", "pear"]
```
```js
const array = [["apple", "orange"], ["lemon", "pear"]];
console.log(Arrays.insertRight(array, {row: 0, column: 0}, "mango"));
// Returns: [["apple", "mango", "orange"], ["lemon", "pear"]]
```
```js
const array = [["apple", "orange"], ["lemon", "pear"]];
const coordinates = new Arrays.Coordinates(0, 0);
console.log(Arrays.insertRight(array, coordinates, "mango"));
// Returns: [["apple", "mango", "orange"], ["lemon", "pear"]]
```
The `insertRight` method returns a same-dimensional array, with one or more items inserted to the right of items specified.
`Settings:`
```js
{
insertAll: false, // inserts the item to the right of all items that match
replace: false // inserts the item in the place of the item to the right
}
```
Insert an object like this at the end of the method to choose your settings.
`Parameters:`
1) 1d+ array
2) Item, index, [array][1], [object][2], or [Coordinates][3]
3) Item
`Returns:` SameD array
### SearchQuery
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.searchQuery(array, new Arrays.ItemConditional({
includes: "a"
})));
// Returns: ["apple", "orange", "pear"]
```
```js
const array = [["apple", "orange"], ["mango", "pear"]];
console.log(Arrays.searchQuery(array, new Arrays.ItemConditional({
includes: "g"
})));
// Returns: ["orange", "mango"]
```
The `searchQuery` method returns a 1d array including all the items in the array that pass one or more conditions.
`Parameters:`
1) 1d+ array
2) [ItemConditional](4)
`Returns:` 1d array
### LengthWith
```js
const array = ["apple", "orange", "orange", "pear"];
console.log(Arrays.lengthWith(array, new Arrays.ItemConditional({
equalto: "orange"
})));
// Returns: 2
```
```js
const array = [["pear", "orange"], ["lemon", "pear"], ["pear", "mango"]];
console.log(Arrays.lengthWith(array, new Arrays.ItemConditional({
equalto: "pear"
})));
// Returns: 3
```
The `lengthWith` method returns the length of an array, where if n item doesn't pass one or more conditions, it doesn't count towards the length.
`Settings:`
```js
{
countArrays: false // counts arrays while getting the total length
}
```
Insert an object like this at the end of the method to choose your settings.
`Parameters:`
1) 1d+ array
2) [ItemConditional](4)
`Returns:` number
## Exclusive 1d Methods
These methods are exclusive to traditional 1-dimensional arrays.
### Table of Contents
- [First](#first) - Get the first number of items of an array
- [Last](#last) - Get the last number of items in an array
- [AllIndexesOf](#allindexesof) - Get a list of indexes of an items
- [Distance](#distance) - Get the distance between 2 items
- [Between](#between) - Get all the items between 2 items
- [Cut](#cut) - Cut an array into 2 equal parts
### First
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.first(array));
// Returns: "apple"
```
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.first(array, 2));
// Returns: ["apple", "orange"]
```
The `first` method returns either the first item from the array or a 1d array including the first number of items.
`Parameters:`
1) 1d array
2) [Optional: 1] The number of first items
`Returns:` Item **or** 1d array
### Last
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.last(array));
// Returns: "pear"
```
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.last(array, 2));
// Returns: ["lemon", "pear"]
```
The `last` method returns either the last item from the array or a 1d array including the last number of items.
`Parameters:`
1) 1d array
2) [Optional: 1] The number of last items
`Returns:` Item **or** 1d array
### AllIndexesOf
```js
const array = ["apple", "lemon", "pear", "lemon"];
console.log(Arrays.allIndexesOf(array, "lemon"));
// Returns: [1, 3]
```
The `allIndexesOf` method returns a 1d array with all the indexes of a specified item.
`Parameters:`
1) 1d array
2) Item
`Returns:` 1d array
### Distance
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.distance(array, "apple", "pear"));
// Returns: 3
```
The `distance` method returns the distance between 2 items in the array.
`Parameters:`
1) 1d array
2) Item
3) Item
`Returns:` number
### Between
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.between(array, "apple", "pear"));
// Returns: ["orange", "lemon"]
```
The `between` method returns a 1d array with the items between 2 items in the array.
`Parameters:`
1) 1d array
2) Item
3) Item
`Returns:` 1d array
### Cut
```js
const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.cut(array));
// Returns: [["apple", "orange"], ["lemon", "pear"]]
```
```js
const array = ["apple", "orange", "mango", "lemon", "pear"];
console.log(Arrays.cut(array));
// Returns: [["apple", "orange", "mango"], ["lemon", "pear"]]
```
The `cut` method returns a 2d array with 2 rows, where each row is one half of the array.
`Parameters:`
1) 1d array
`Returns:` 2d array
## Exclusive 2d+ Methods
These methods are exclusive to 2d arrays and above.
Note that all examples will use 2d arrays, but you can still use 3d arrays.
### Common Terms
You probably know this already, but just incase:
- A `row` is each array item in a 2d+ array.
- A `column` is an array of a specific index in each row.
- A `layer` is an array of a specific index in each column.
### Table of Contents
- [Row](#row) - Get a specific row of an array
- [Column](#column) - Get a specific column of an array
- [Columns](#columns) - Get the number of columns
- [FindItem](#finditem) - Find an item using coordinates
- [FindCoordinates](#findcoordinates) - Find the coordinates of an item
- [FindMultipleCoordinates](#findmultiplecoordinates) - Get a list of coordinates of an item
- [InsertRowAbove](#insertrowabove) - Insert a row above a specified row
- [InsertRowBelow](#insertrowbelow) - Insert a row below a specified row
- [InsertColumnLeft](#insertcolumnleft) - Insert a column left of a specified column
- [InsertColumnRight](#insertcolumnright) - Insert a column right of a specified column
- [LocalIndexOf](#localindexof) - Get the local index of an item
- [AllLocalIndexesOf](#alllocalindexesof) - Get a list of local indexes of an item
- [TotalLength](#totallength) - Get the total length of an array
- [SearchRowQuery](#searchrowquery) - Get all the items that match one or more conditions in a specified row
- [SearchColumnQuery](#searchcolumnquery) - Get all the items that match one or more conditions in a specified column
- [RowIncludes](#rowincludes) - Get if a row includes an item
- [ColumnIncludes](#columnincludes) - Get if a column includes an item
- [ReplaceInRow](#replaceinrow) - Replace items in a specified row
- [ReplaceInColumn](#replaceincolumn) - Replace items in a specified column
- [ReplaceInAllRows](#replaceinallrows) - Replace items in every row
- [RowOf](#rowof) - Get the row of an item in an array
- [ColumnOf](#columnof) - Get the column of an item in an array
- [SortRow](sortrow) - Sort a specified row in an array
- [SortColumn](#sortcolumn) - Sort a specified column in an array
- [SortAllRows](#sortallrows) - Sort every row in an array
### Row
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.row(array, 0));
// Returns: ["apple", "orange"]
```
The `row` method returns a row in the array.
`Parameters:`
1) 2d+ array
2) Index
`Returns:` 1d+ array
### Column
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.column(array, 0));
// Returns: ["apple", "lemon"]
```
The `column` method returns a column in the array.
`Parameters:`
1) 2d+ array
2) Index
`Returns:` 1d+ array
### Columns
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.columns(array));
// Returns: 2
```
```js
const array = [
["apple", "orange"],
["lemon", "pear", "lime"]
];
console.log(Arrays.columns(array, "max"));
// Returns: 3
```
```js
const array = [
["apple", "orange"],
["lemon", "pear", "lime"]
];
console.log(Arrays.columns(array, "min"));
// Returns: 2
```
The `columns` method returns how many columns are inside the array. If the type is "max", it'll include columns where not every item is defined. If the type is "min", it'll only include columns where every item is defined.
`Parameters:`
1) 2d+ array
2) [Optional: "max"] Type
`Returns:` number
### FindItem
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.findItem(array, [0, 1]));
// Returns: "orange"
```
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.findItem(array, {row: 0, column: 1}));
// Returns: "orange"
```
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
const coordinates = new Arrays.Coordinates(0, 1);
console.log(Arrays.findItem(array, coordinates));
// Returns: "orange"
```
The `findItem` method returns an item found in the array.
`Parameters:`
1) 2d+ array
2) Item, index, [array][1], [object][2], or [Coordinates][3]
`Returns:` Item
### FindCoordinates
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.findCoordinates(array, "lemon"));
// Returns: Coordinates { row: 1, column: 0 }
```
The `findCoordinates` method returns a Coordinates constructor of the coordinates of an item in the array.
`Parameters:`
1) 2d+ array
2) Item
`Returns:` [Coordinates][3]
### FindMultipleCoordinates
```js
const array = [
["pear", "orange"],
["lemon", "pear"]
];
console.log(Arrays.findMultipleCoordinates(array, "pear"));
// Returns: [Coordinates { row: 0, column: 0 }, Coordinates { row: 1, column: 1 }]
```
The `findCoordinates` method returns a 1d array of Coordinates constructors of the coordinates of each instance of an item in the array.
`Parameters:`
1) 2d+ array
2) Item
`Returns:` 1d array
### InsertRowAbove
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.insertRowAbove(array, 0, ["mango", "kiwi"]));
/* Returns:
[
["mango", "kiwi"],
["apple", "orange"],
["lemon", "pear"]
]
```
The `insertRowAbove` method returns a same-dimensional array with a new row inserted above a specified row.
`Settings:`
```js
{
replace: false // inserts the row in the place of the row above
}
```
`Parameters:`
1) 2d+ array
2) Index
3) 1d+ array
`Returns:` SameD array
### InsertRowBelow
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.insertRowBelow(array, 0, ["mango", "kiwi"]));
/* Returns:
[
["apple", "orange"],
["mango", "kiwi"],
["lemon", "pear"]
]
```
The `insertRowBelow` method returns a same-dimensional array with a new row inserted below a specified row.
`Settings:`
```js
{
replace: false // inserts the row in the place of the row above
}
```
`Parameters:`
1) 2d+ array
2) Index
3) 1d+ array
`Returns:` SameD array
### InsertColumnLeft
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.insertColumnLeft(array, 0, ["mango", "kiwi"]));
/* Returns:
[
["mango", "apple", "orange"],
["kiwi", "lemon", "pear"]
]
```
The `insertColumnLeft` method returns a same-dimensional array with a new column inserted to the left of a specified column.
`Settings:`
```js
{
replace: false // inserts the column in the place of the column to the left
}
```
`Parameters:`
1) 2d+ array
2) Index
3) 1d+ array
`Returns:` SameD array
### InsertColumnRight
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.insertColumnRight(array, 0, ["mango", "kiwi"]));
/* Returns:
[
["apple", "mango", "orange"],
["lemon", "kiwi", "pear"]
]
```
The `insertColumnRight` method returns a same-dimensional array with a new column inserted to the right of a specified column.
`Settings:`
```js
{
replace: false // inserts the column in the place of the column to the right
}
```
`Parameters:`
1) 2d+ array
2) Index
3) 1d+ array
`Returns:` SameD array
### LocalIndexOf
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.localIndexOf(array, "orange"));
// Returns: 1
```
The `localIndexOf` method returns the index of the item in its most local array.
`Parameters:`
1) 2d+ array
2) Item
`Returns:` number
### AllLocalIndexesOf
```js
const array = [
["pear", "orange"],
["lemon", "pear"]
];
console.log(Arrays.localIndexOf(array, "pear"));
// Returns: [0, 1]
```
The `allLocalIndexesOf` method returns a 1d array of the local index of each instance of a specified item in the array.
`Parameters:`
1) 2d+ array
2) Item
`Returns:` 1d array
### TotalLength
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.totalLength(array));
// Returns: 4
```
The `totalLength` method returns the total length of the array. It adds up the length of every single item in the array.
`Settings:`
```js
{
countArrays: false // counts arrays while getting the total length
}
```
Insert an object like this at the end of the method to choose your settings.
`Parameters:`
1) 2d+ array
`Returns:` number
### SearchRowQuery
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.searchRowQuery(array, 0, new Arrays.ItemConditional({
includes: "r"
})));
// Returns: ["orange"]
```
The `searchRowQuery` method returns a 1d+ array including all the items in a specified row that pass one or more conditions.
`Parameters:`
1) 2d+ array
2) Index
3) [ItemConditional](4)
`Returns:` 1d+ array
### SearchColumnQuery
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.searchColumnQuery(array, 0, new Arrays.ItemConditional({
notincludes: "a"
})));
// Returns: ["lemon"]
```
The `searchColumnQuery` method returns a 1d+ array including all the items in a specified column that pass one or more conditions.
`Parameters:`
1) 2d+ array
2) Index
3) [ItemConditional](4)
`Returns:` 1d+ array
### RowIncludes
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.rowIncludes(array, 0, "orange"));
// Returns: true
```
The `rowIncludes` method returns a boolean depending on whether or not a specified row includes an item.
`Parameters:`
1) 2d+ array
2) Index
3) Item
`Returns:` boolean
### ColumnIncludes
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.columnIncludes(array, 0, "pear"));
// Returns: false
```
The `columnIncludes` method returns a boolean depending on whether or not a specified column includes an item.
`Parameters:`
1) 2d+ array
2) Index
3) Item
`Returns:` boolean
### ReplaceInRow
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.replaceInRow(array, 0, "orange", "kiwi"));
/* Returns:
[
["apple", "kiwi"],
["lemon", "pear"]
];
```
The `replaceInRow` method returns a 2d+ array, with one or more items replaced in a specified row.
`Parameters:`
1) 2d+ array
2) Index
3) Item
4) Item
`Returns:` 2d+ array
### ReplaceInColumn
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.replaceInRow(array, 0, "lemon", "mango"));
/* Returns:
[
["apple", "orange"],
["mango", "pear"]
];
```
The `replaceInRow` method returns a 2d+ array, with one or more items replaced in a specified row.
`Parameters:`
1) 2d+ array
2) Index
3) Item
4) Item
`Returns:` 2d+ array
### ReplaceInAllRows
```js
const array = [
["pear", "orange"],
["lemon", "pear"]
];
console.log(Arrays.replaceInAllRows(array, "pear", "kiwi"));
/* Returns:
[
["kiwi", "orange"],
["lemon", "kiwi"]
];
```
The `replaceInAllRows` method returns a 2d+ array, with one or more items replaced in each row.
`Parameters:`
1) 2d+ array
2) Item
3) Item
`Returns:` 2d+ array
### RowOf
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.rowOf(array, "orange"));
// Returns: 0
```
The `rowOf` method returns the row of a specified item in the array.
`Parameters:`
1) 2d+ array
2) Item
`Returns:` number
### ColumnOf
```js
const array = [
["apple", "orange"],
["lemon", "pear"]
];
console.log(Arrays.columnOf(array, "pear"));
// Returns: 1
```
The `columnOf` method returns the column of a specified item in the array.
`Parameters:`
1) 2d+ array
2) Item
`Returns:` number
### SortRow
```js
const array = [
[1, 2, 3],
[3, 2, 1]
];
console.log(Arrays.sortRow(array, 0, (a, b) => a - b));
/* Returns:
[
[1, 2, 3],
[1, 2, 3]
]
```
The `sortRow` method returns a same-dimensional array with a specified row sorted.
`Parameters:`
1) 2d+ array
2) Index
3) Function
`Returns:` 2d+ array
### SortColumn
```js
const array = [
[1, 2, 3],
[3, 2, 1]
];
console.log(Arrays.sortColumn(array, 2, (a, b) => a - b));
/* Returns:
[
[1, 2, 1],
[1, 2, 3]
]
```
The `sortColumn` method returns a same-dimensional array with a specified column sorted.
`Parameters:`
1) 2d+ array
2) Index
3) Function
`Returns:` 2d+ array
### SortAllRows
```js
const array = [
[5, 4, 6],
[3, 5, 2]
];
console.log(Arrays.sortAllRows(array, (a, b) => a - b));
/* Returns:
[
[4, 5, 6],
[2, 3, 5]
]
```
The `sortAllRows` method returns a same-dimensional array with all rows sorted.
`Parameters:`
1) 2d+ array
2) Index
3) Function
`Returns:` 2d+ array
## Exclusive 3d+ Methods
These methods are exclusive to 3d arrays.
While technically these can work on 4d arrays and above, they may be unstable.
### Table of Contents
- [Layer](#layer) - Get a layer in an array
- [Layers](#layers) - Get the number of layers
- [InsertLayerLeft](#insertlayerleft) - Insert a layer left of a specified layer
- [InsertLayerRight](#insertlayerright) - Insert a layer right of a specified layer
- [SearchLayerQuery](#searchlayerquery) - Get all the items that match one or more conditions in a specified layer
- [LayerIncludes](#layerincludes) - Get if a layer includes an item
- [ReplaceInLayer](#replaceinlayer) - Replace items in a specified layer
- [SortLayer](#sortlayer) - Sort items in a specified layer
### Layer
```js
const array = [
[
["apple", "orange"],
["lemon", "pear"]
],
[
["banana", "avocado"],
["peach", "grape"]
]
];
console.log(Arrays.layer(array, 0));
// Returns: ["apple", "lemon", "banana", "peach"]
```
The `layer` method returns a 1d+ array of a specified layer in the array.
`Parameters:`
1) 3d+ array
2) Index
`Returns:` 1d+ array
### Layers
```js
const array1 = [
[
["apple", "orange"],
["lemon", "pear"]
],
[
["banana", "avocado"],
["peach", "grape"]
]
];
const array2 = [
[
["apple", "orange"],
["lemon", "pear", "kiwi"]
],
[
["banana", "avocado", "lime"],
["peach", "grape"]
]
];
```
```js
console.log(Arrays.layers(array1));
// Returns: 2
```
```js
console.log(Arrays.layers(array2, "max"));
// Returns: 3
```
```js
console.log(Arrays.layers(array2, "min"));
// Returns: 2
```
The `layers` method returns the number of layers in the array. The type (either "min" or "max") works the same way as the [columns](#columns) method.
`Parameters:`
1) 3d+ array
2) [Optional: "max"] Type
`Returns:` number
### InsertLayerLeft
```js
const array = [
[
["apple", "orange"],
["lemon", "pear"]
],
[
["banana", "avocado"],
["peach", "grape"]
]
];
console.log(Arrays.insertLayerLeft(array, 1, ["kiwi", "mango", "lime", "durian"]));
/* Returns:
[
[
["apple", "kiwi", "orange"],
["lemon", "mango", "pear"]
],
[
["banana", "lime", "avocado"],
["peach", "durian", "grape"]
]
]
```
The `insertLayerLeft` method returns a 3d+ array with a layer inserted to the left of a specified layer.
`Parameters:`
1) 3d+ array
2) Index
3) 1d+ array
`Returns:` 3d+ array
### InsertLayerRight
```js
const array = [
[
["apple", "orange"],
["lemon", "pear"]
],
[
["banana", "avocado"],
["peach", "grape"]
]
];
console.log(Arrays.insertLayerRight(array, 0, ["kiwi", "mango", "lime", "durian"]));
/* Returns:
[
[
["apple", "kiwi", "orange"],
["lemon", "mango", "pear"]
],
[
["banana", "lime", "avocado"],
["peach", "durian", "grape"]
]
]
```
The `insertLayerRight` method returns a 3d+ array with a layer inserted to the right of a specified layer.
`Parameters:`
1) 3d+ array
2) Index
3) 1d+ array
`Returns:` 3d+ array
### SearchLayerQuery
```js
const array = [
[
["apple", "orange"],
["lemon", "pear"]
],
[
["banana", "avocado"],
["peach", "grape"]
]
];
console.log(Arrays.searchLayerQuery(array, 0, new Arrays.ItemConditional({
includes: "a"
})));
// Returns: ["apple", "banana", "peach"]
```
The `searchLayerQuery` method returns a 1d+ array including all the items in a specified layer that pass one or more conditions.
`Parameters:`
1) 3d+ array
2) Index
3) [ItemConditional](4)
`Returns:` 1d+ array
### LayerIncludes
```js
const array = [
[
["apple", "orange"],
["lemon", "pear"]
],
[
["banana", "avocado"],
["peach", "grape"]
]
];
console.log(Arrays.layerIncludes(array, 1, "avocado"));
// Returns: true
```
The `layerIncludes` method returns a boolean depending on whether or not a specified layer includes an item.
`Parameters:`
1) 3d+ array
2) Index
3) Item
`Returns:` boolean
### ReplaceInLayer
```js
const array = [
[
["apple", "orange"],
["lemon", "pear"]
],
[
["banana", "avocado"],
["peach", "grape"]
]
];
console.log(Arrays.replaceInLayer(array, 0, "banana", "kiwi"));
/* Returns:
[
[
["apple", "orange"],
["lemon", "pear"]
],
[
["kiwi", "avocado"],
["peach", "grape"]
]
]
```
The `replaceInLayer` method returns a 3d+ array, with one or more items replaced in a specified layer.
`Parameters:`
1) 3d+ array
2) Index
3) Item
4) Item
`Returns:` 3d+ array
### SortLayer
```js
const array = [
[
[1, 2],
[5, 3],
],
[
[6, 2],
[3, 8]
]
];
console.log(Arrays.sortLayer(array, 0, (a, b) => a - b));
/* Returns:
[
[
[1, 2],
[3, 3],
],
[
[5, 2],
[6, 8]
]
]
```
The `sortLayer` method returns a 3d+ array, with a specified layer sorted.
`Parameters:`
1) 3d+ array
2) Index
3) Item
4) Item
`Returns:` 3d+ array
## Coordinates Class
The `Coordinates` constructor is easy and useful.
```js
const coordinates1d = new Arrays.Coordinates(0);
// Row = 0
const coordinates2d = new Arrays.Coordinates(0, 0);
// Row = 0, Column = 0
const coordinates3d = new Arrays.Coordinates(0, 0, 0);
// Row = 0, Column = 0, Layer = 0
```
You can use it in a variety of ways:
### FindItemInArray
```js
const array = [
["apple", "orange", "kiwi"],
["mango", "banana", "pear"]
];
const coordinates = new Arrays.Coordinates(1, 1);
console.log(coordinates.findItemInArray(array));
// Returns: "banana"
```
The `findItemInArray` method finds an item in an array based on the coordinates. If it can't find the item, it'll return undefined.
`Parameters:`
1) 1d+ array
`Returns:` Item
### SetItemInArray
```js
const array = [
["apple", "orange", "kiwi"],
["mango", "banana", "pear"]
];
const coordinates = new Arrays.Coordinates(1, 1);
console.log(coordinates.setItemInArray(array, "plum"));
/* Returns:
[
["apple", "orange", "kiwi"],
["plum", "banana", "pear"]
]
```
The `setItemInArray` method sets an item in an array based on the coordinates.
`Parameters:`
1) 1d+ array
2) Item
`Returns:` 1d+ array
### IncreaseLocal
```js
const coordinates = new Arrays.Coordinates(0);
console.log(coordinates.increaseLocal(1));
// Returns: Coordinates { row: 1 }
```
```js
const coordinates = new Arrays.Coordinates(1, 1);
console.log(coordinates.increaseLocal(1));
// Returns: Coordinates { row: 1, column: 2 }
```
```js
const coordinates = new Arrays.Coordinates(1, 0, 2);
console.log(coordinates.increaseLocal(1));
// Returns: Coordinates { row: 1, column: 0, layer: 3 }
```
The `increaseLocal` method increases the most local array of the deepest item by an amount.
`Parameters:`
1) Number
`Returns:` Coordinates
### IncreaseRow
```js
const coordinates = new Arrays.Coordinates(0, 1);
console.log(coordinates.increaseRow(1));
// Returns: Coordinates { row: 1, column: 1 }
```
The `increaseRow` method increases the row of the Coordinates by an amount.
`Parameters:`
1) Number
`Returns:` Coordinates
### IncreaseColumn
```js
const coordinates = new Arrays.Coordinates(1, 1);
console.log(coordinates.increaseColumn(2));
// Returns: Coordinates { row: 1, column: 3 }
```
The `increaseColumn` method increases the column of the Coordinates by an amount. If the Coordinates doesn't have a column, it'll automatically be set to the amount.
`Parameters:`
1) Number
`Returns:` Coordinates
### IncreaseLayer
```js
const coordinates = new Arrays.Coordinates(0, 1, 1);
console.log(coordinates.increaseLayer(1));
// Returns: Coordinates { row: 0, column: 1, layer: 2 }
```
The `increaseLayer` method increases the layer of the Coordinates by an amount. If the Coordinates doesn't have a layer, it'll automatically be set to the amount. If the Coordinates doesn't have a column or layer, the column will be set to 0, and the layer will be set to the amount.
`Parameters:`
1) Number
`Returns:` Coordinates
### IncreaseAll
```js
const coordinates = new Arrays.Coordinates(1, 0, 1);
console.log(coordinates.increaseAll(1));
// Returns: Coordinates { row: 2, column: 1, layer: 2 }
```
The `increseAll` method increases the row, column, and layer by an amount. If the Coordinates doesn't have a column or a layer, they'll automatically be set to the amount.
`Parameters:`
1) Number
`Returns:` Coordinates
### IncreaseBy
```js
const coordinates1 = new Arrays.Coordinates(1, 2, 3);
const coordinates2 = [3, 2, 1];
console.log(coordinates1.increaseBy(coordinates2));
// Returns: Coordinates { row: 4, column: 4, layer: 4 }
```
```js
const coordinates1 = new Arrays.Coordinates(1, 2, 3);
const coordinates2 = {row: 3, column: 2, layer: 1};
console.log(coordinates1.increaseBy(coordinates2));
// Returns: Coordinates { row: 4, column: 4, layer: 4 }
```
```js
const coordinates1 = new Arrays.Coordinates(1, 2, 3);
const coordinates2 = new Arrays.Coordinates(3, 2, 1);
console.log(coordinates1.increaseBy(coordinates2));
// Returns: Coordinates { row: 4, column: 4, layer: 4 }
```
The `increaseBy` method increases the row, column, and layer by another coordinates' row, column, and layer. If the Coordinates doesn't have a column or layer, they'll automatically be set to the other coordinates'.
`Parameters:`
1) [Array][1], [object][2], or [Coordinates][3]
`Returns:` Coordinates
### ShiftLeft
```js
const coordinates = new Arrays.Coordinates(0, 1, 2);
console.log(coordinates.shiftLeft());
// Returns: Coordinates { row: 1, column: 2, layer: 0 }
```
The `shiftLeft` method shifts each value 1 place to the left. The row is the value of the column, the column is the value of the layer, and the layer is the value of the row.
`Returns:` Coordinates
### ShiftRight
```js
const coordinates = new Arrays.Coordinates(0, 1, 2);
console.log(coordinates.shiftRight());
// Returns: Coordinates { row: 2, column: 0, layer: 1 }
```
The `shiftRight` method shifts each value 1 place to the right. The row is the value of the layer, the column is the value of the row, and the layer is the value of the column.
`Returns:` Coordinates
### LoseColumn
```js
const coordinates = new Arrays.Coordinates(2, 1);
console.log(coordinates.loseColumn());
// Returns: Coordinates { row: 2 }
```
The `loseColumn` method deletes the column from the Coordinates. If there's a layer in the Coordinates, that is deleted as well.
`Returns:` Coordinates
### LoseLayer
```js
const coordinates = new Arrays.Coordinates(1, 0, 2);
console.log(coordinates.loseLayer());
// Returns: Coordinates { row: 2, column: 0 }
```
The `loseLayer` method deletes the layer from the Coordinates.
`Returns:` Coordinates
### ToArray
```js
const coordinates = new Arrays.Coordinates(1, 2, 3);
console.log(coordinates.toArray());
// Returns: [1, 2, 3]
```
The `toArray` method converts the Coordinates to an array.
`Returns:` [Array][1]
### ToObject
```js
const coordinates = new Arrays.Coordinates(1, 2, 3);
console.log(coordinates.toObject());
// Returns: { row: 1, column: 2, layer: 3 }
```
The `toObject` method converts the Coordinates to an object.
`Returns:` [Object][2]
## ItemConditional Class
The `ItemConditional` constructor is used for searchQuery and similar methods.
```js
const condition = new Arrays.ItemConditional({
equalto: "apple"
});
```
Here are all the different conditions you can check:
```js
{
equalto: "apple" // checks if the item is equal to this (==)
notequalto: "orange" // checks if the item is not equal to this (!=)
typeequalto: "kiwi" // checks if the item is type equal to this (===)
nottypeequalto: "mango" // checks if the item is not type equal to this (!==)
greaterthan: 68 // checks if the item is greater than this (>)
lessthan: 70 // checks if the item is less than this (<)
greaterthanorequalto: 5 // checks if the item is greater than or equal to this (>=)
lessthanorequalto: 10 // checks if the item is less than or equal to this (<=)
includes: "e" // checks if the item includes this (.includes)
notincludes: "o" // checks if the item doesn't include this (!.includes)
arrayincludes: ["banana", "avocado"] // checks if this array includes the item ([].includes)
arraynotincludes: ["pear", "grape"] // checks if this array doesn't include the item (![].includes)
}
```
### CheckConditions
```js
const condition = new Arrays.ItemConditional({
greaterthan: 68,
lessthan: 70
});
const array = [67, 68, 69, 70, 71, 72];
console.log(condition.checkConditions(array[0]));
// Returns: false
console.log(condition.checkConditions(array[2]));
// Returns: true
```
The `checkConditions` method checks an item against the conditions. The item has to pass *all* the conditions to return true.
`Parameters:`
1) Item
`Returns:` boolean
### AddCondition
```js
const condition = new Arrays.ItemConditional({
greaterthan: 68,
lessthan: 70
});
condition.addCondition({
includes: "seed"
});
console.log(condition);
/* Returns: ItemConditional { conditions: {
greaterthan: 68,
lessthan: 70,
includes: "seed"
}}
```
The `addCondition` method adds a condition to the ItemConditional. If the condition of that type already exists, it'll replace it.
`Parameters:`
1) Object
`Returns:` void
### RemoveCondition
```js
const condition = new Arrays.ItemConditional({
greaterthan: 68,
lessthan: 70,
includes: "seed"
});
condition.removeCondition("includes");
console.log(condition);
/* Returns: ItemConditional { conditions: {
greaterthan: 68,
lessthan: 70
}}
```
The `removeCondition` method removes a condition from the ItemConditional. If the condition of that type doesn't exist, it won't do anything.
`Parameters:`
1) String
`Returns:` void
## Extras
Other stuff that you might need.
### Array Coordinates
You can use an array in some methods to input coordinates.
```js
[0] // Row: 0
[0, 1] // Row: 0, Column: 1
[0, 1, 2] // Row: 0, Column: 1, Layer: 2
```
You don't need to input the column or layer.
### Object Coordinates
You can use an object in some methods to input coordinates.
```js
{ row: 0 } // Row: 0
{ row: 0, column: 1 } // Row: 0, Column: 1
{ row: 0, column: 1, layer: 2 } // Row: 0, Column: 1, Layer: 2
```
You don't need to input the column or layer.
[0]: #arraycoordinates
[1]: #objectcoordinates
[2]: #coordinatesclass