Click or drag to resize

Telogis.GeoBase.Arrays

Verizon Connect Logo
Print this page
Learn more about Verizon Connect GeoBase.
Get information about the latest release
Arrays Namespace

A namespace enclosing GeoBase functions for manipulating arrays. Normally, these would be attached to the Array prototype, but this is avoided to maintain compatibility with some other libraries.

Functions
NameDescription
contains (Array arr, Object item)

Checks to see whether a given array contains a certain item.

Arguments
  • arr (Array) - The array to check for Arrays.contains.item in.

  • item (Object) - The item to check for in Arrays.contains.arr.

Returns

Boolean - True if Arrays.contains.item is an entry in Arrays.contains.arr; false otherwise.

count (Array arr)

Finds the number of items in an array (or object) using hasOwnProperty(), such that in the case of sparse arrays, the actual number of elements in the array is returned.

Arguments
  • arr (Array) - The array to count the properties of.

Returns

Number - The number of non-prototyped properties of the array.

indexOf (Array arr, Object item)

Finds the index of a certain item in an array.

Arguments
  • arr (Array) - The array to search.

  • item (Object or Function) - The item to search for. Or, a boolean function that determines whether a passed item in the array is the desired one. When used as a function, this acts as a predicate to find the first index in the array that matches the item. If found, indexOf returns the index. If no match is found, returns -1.

Returns

Number - The index of Arrays.indexOf.item in Arrays.indexOf.arr, or -1 if no match was found.

intersection (Array ...)

Finds the intersection of multiple arrays.

Arguments
  • ... (Array) - A list of arrays to take the intersection of.

Returns

Array - An array / set containing only the elements found in each of the argument arrays.

move (Array arr, Number from, Number to)

Moves an entry of an array from a specified index to another. The supplied array instance is not modified in the call.

Arguments
  • arr (Array) - The array to move an item in.

  • from (Number) - The original index of the item to move.

  • to (Number) - The index to move the item to.

Returns

Array - The modified array.

place (Array arr, Object item, Function comparator)

Assuming the array is already sorted by Arrays.place.comparator, inserts a new item in the array at the appropriate location. If there are multiple appropriate locations, the first is used. The supplied instance of the array is not modified.

Arguments
  • arr (Array) - The sorted array to insert an item in.

  • item (Object) - The item to insert into the sorted array.

  • (Optional) comparator (Function) - The callback used to compare two entries in the array. Defaults to function (a, b) {return a - b;}.

    Arguments
    • a (Object) - The first item in the array to compare.

    • b (Object) - The second item in the array to compare.

    Returns

    Number - A negative number if Arrays.place.comparator.a occurs before Arrays.place.comparator.b in the sorted array, and a positive number if the converse is true. If Arrays.place.comparator.a and Arrays.place.comparator.b are equivalent, this should return 0.

Returns

Array - The modified array.

query (Array arr, Function condition)

Finds the index of the first item in the array to satisfy a certain condition.

Arguments
  • arr (Array) - The array to search.

  • condition (Function) - The boolean function that must be satisfied by the result of the search. If met, query returns the index. If not met, returns -1.

    Arguments
    • item (Object) - The value of any given item in the array.

    Returns

    Boolean - True if Arrays.query.condition.item satisfies the desired condition (and should have its index returned); false otherwise.

Returns

Number - The index of the first item satisfying Arrays.query.condition in Arrays.query.arr, or -1 if no match was found.

search (Array arr, Object item, Function comparator)

Performs a binary search on a sorted array to check which index a new item should be inserted at to maintain the order specified by Arrays.search.comparator.

Arguments
  • arr (Array) - The array to search.

  • item (Object) - The arbitrary item / object to find an index for in the sorted array. Returns true if the object is found in the array; otherwise false.

  • (Optional) comparator (Function) - The callback used to compare two entries in the array. Defaults to function (a, b) {return a - b;}.

    Arguments
    • a (Object) - The first item in the array to compare.

    • b (Object) - The second item in the array to compare.

    Returns

    Number - A negative number if Arrays.search.a occurs before Arrays.search.b in the sorted array, and a positive number if the converse is true. If Arrays.search.a and Arrays.search.b are equivalent, this should return 0.

Returns

Number - The index that the new item should be inserted at to maintain order.

swap (Array arr, Number i, Number j)

Exchanges two values in an array. The supplied array instance is not modified during the call.

Arguments
  • arr (Array) - The array to swap items in.

  • i (Number) - The index of the first item to swap.

  • j (Number) - The index of the second item to swap.

Returns

Array - The modified array.

toSet (Array arr)

Finds a set corresponding to a given array -- i.e. that array with duplicate values removed.

Arguments
  • arr (Array) - The array to remove duplicates from.

Returns

Array - The set representation of the array.

toString (Array arr)

Finds a string representation of an array.

Arguments
  • arr (Array) - The array to represent as a string.

Returns

String - A string of the form '[a1, a2, ..., an]'.