2.1.0 • Published 3 years ago

arraymethods.js v2.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

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

To install the arrayMethods.js package, simply do:

npm install arraymethods.js

Using the Package

Using the package is simple and easy!

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 - Get the dimension of an array
  • Swap - Swap 2 items
  • Replace - Replace items
  • InsertLeft - Insert an item left of an another item
  • InsertRight - Insert an item right of an another item
  • SearchQuery - Get all the items that match one or more conditions
  • LengthWith - Like searchQuery, but it returns the length with one or more CheckConditions

GetDimension

const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.getDimension(array));
// Returns: 1
const array = [["apple", "orange"], ["lemon", "pear"]];
console.log(Arrays.getDimension(array));
// Returns: 2
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

const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.swap(array, "apple", "lemon"));
// Returns: ["lemon", "orange", "apple", "pear"]
const array = [["apple", "orange"], ["lemon", "pear"]];
console.log(Arrays.swap(array, {row: 0, column: 0}, {row: 1, column: 0}));
// Returns: [["lemon", "orange"], ["apple", "pear"]]
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, object, or Coordinates 3) Item, index, array, object, or Coordinates

Returns: SameD array

Replace

const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.replace(array, "apple", "lemon"));
// Returns: ["lemon", "orange", "lemon", "pear"]
const array = [["apple", "orange"], ["lemon", "pear"]];
console.log(Arrays.replace(array, {row: 1, column: 0}, "lemon"));
// Returns: [["lemon", "orange"], ["lemon", "pear"]]
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, object, or Coordinates 3) Item

Returns: SameD array

InsertLeft

const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.insertLeft(array, "orange", "mango"));
// Returns: ["apple", "mango", "orange", "lemon", "pear"]
const array = [["apple", "orange"], ["lemon", "pear"]];
console.log(Arrays.insertLeft(array, {row: 0, column: 1}, "mango"));
// Returns: [["apple", "mango", "orange"], ["lemon", "pear"]]
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:

{
  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, object, or Coordinates 3) Item

Returns: SameD array

InsertRight

const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.insertRight(array, "apple", "mango"));
// Returns: ["apple", "mango", "orange", "lemon", "pear"]
const array = [["apple", "orange"], ["lemon", "pear"]];
console.log(Arrays.insertRight(array, {row: 0, column: 0}, "mango"));
// Returns: [["apple", "mango", "orange"], ["lemon", "pear"]]
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:

{
  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, object, or Coordinates 3) Item

Returns: SameD array

SearchQuery

const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.searchQuery(array, new Arrays.ItemConditional({
  includes: "a"
})));
// Returns: ["apple", "orange", "pear"]
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

Returns: 1d array

LengthWith

const array = ["apple", "orange", "orange", "pear"];
console.log(Arrays.lengthWith(array, new Arrays.ItemConditional({
  equalto: "orange"
})));
// Returns: 2
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:

{
  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

Returns: number

Exclusive 1d Methods

These methods are exclusive to traditional 1-dimensional arrays.

Table of Contents

  • First - Get the first number of items of an array
  • Last - Get the last number of items in an array
  • AllIndexesOf - Get a list of indexes of an items
  • Distance - Get the distance between 2 items
  • Between - Get all the items between 2 items
  • Cut - Cut an array into 2 equal parts

First

const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.first(array));
// Returns: "apple"
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

const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.last(array));
// Returns: "pear"
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

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

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

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

const array = ["apple", "orange", "lemon", "pear"];
console.log(Arrays.cut(array));
// Returns: [["apple", "orange"], ["lemon", "pear"]]
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

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

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

const array = [
  ["apple", "orange"],
  ["lemon", "pear"]
];
console.log(Arrays.columns(array));
// Returns: 2
const array = [
  ["apple", "orange"],
  ["lemon", "pear", "lime"]
];
console.log(Arrays.columns(array, "max"));
// Returns: 3
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

const array = [
  ["apple", "orange"],
  ["lemon", "pear"]
];
console.log(Arrays.findItem(array, [0, 1]));
// Returns: "orange"
const array = [
  ["apple", "orange"],
  ["lemon", "pear"]
];
console.log(Arrays.findItem(array, {row: 0, column: 1}));
// Returns: "orange"
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, object, or Coordinates

Returns: Item

FindCoordinates

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

FindMultipleCoordinates

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

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:

{
  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

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:

{
  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

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:

{
  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

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:

{
  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

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

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

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:

{
  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

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

Returns: 1d+ array

SearchColumnQuery

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

Returns: 1d+ array

RowIncludes

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

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

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

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

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

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

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

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

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

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

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

const array1 = [
  [
    ["apple", "orange"],
    ["lemon", "pear"]
  ],
  [
    ["banana", "avocado"],
    ["peach", "grape"]
  ]
];
const array2 = [
  [
    ["apple", "orange"],
    ["lemon", "pear", "kiwi"]
  ],
  [
    ["banana", "avocado", "lime"],
    ["peach", "grape"]
  ]
];
console.log(Arrays.layers(array1));
// Returns: 2
console.log(Arrays.layers(array2, "max"));
// Returns: 3
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 method.

Parameters: 1) 3d+ array 2) Optional: "max" Type

Returns: number

InsertLayerLeft

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

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

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

Returns: 1d+ array

LayerIncludes

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

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

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.

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

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

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

const coordinates = new Arrays.Coordinates(0);
console.log(coordinates.increaseLocal(1));
// Returns: Coordinates { row: 1 }
const coordinates = new Arrays.Coordinates(1, 1);
console.log(coordinates.increaseLocal(1));
// Returns: Coordinates { row: 1, column: 2 }
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

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

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

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

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

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 }
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 }
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, object, or Coordinates

Returns: Coordinates

ShiftLeft

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

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

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

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

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

ToObject

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

ItemConditional Class

The ItemConditional constructor is used for searchQuery and similar methods.

const condition = new Arrays.ItemConditional({
  equalto: "apple"
});

Here are all the different conditions you can check:

{
  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

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

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

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.

[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.

{ 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.

2.1.0

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.13

3 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.11

3 years ago

2.0.7

3 years ago

2.0.12

3 years ago

2.0.6

3 years ago

2.0.9

3 years ago

2.0.10

3 years ago

2.0.8

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago