Array functions

The following functions are array specific.

first

Get the first item(s) in an array

Parameters

  • array (type: Array) - The array to select from
  • count (type: Number) - The number of items to retrieve (optional)
  • reverse (type: Boolean) - Whether to reverse the returned array (optional)

Returns

  • (type: Any) - Either first item or an array of first x items

Example

const arr = [1, 2, 3, 4, 5]

$.first(arr) // [1]
$.first(arr, 2) // [1, 2]
$.first(arr, 2, true) // [2, 1]

// Also works with node arrays generated by the qsa function!
$foos = $.qsa('.foo')
$firstFoo = $.first($foos)

last

Get the last item(s) in an array

Parameters

  • array (type: Array) - The array to select from
  • count (type: Number) - The number of items to retrieve (optional)
  • reverse (type: Boolean) - Whether to reverse the returned array (optional)

Returns

  • (type: Any) - Either last item or an array of last x items

Example

const arr = [1, 2, 3, 4, 5]

$.last(arr) // [5]
$.last(arr, 2) // [4, 5]
$.last(arr, 2, true) // [5, 4]

// Also works with node arrays generated by the qsa function!
$foos = $.qsa('.foo')
$lastFoo = $.last($foos)

strip

Remove value(s) from an array

Parameters

  • array (type: Array) - The array to select from
  • values (type: Any) - The values to remove

Returns

  • (type: Array) - The new array

Example

const arr = [1, 2, 3, 4, 5]

$.strip(arr, 2) // [1, 3, 4, 5]
$.strip(arr, 2, 5) // [1, 3, 4]

types

Get the type of each value in an array

Parameters

  • array (type: Array) - The array to use

Returns

  • (type: Array) - An array of item types

Example

const arr = [1, true, 'foo', {}, Symbol(), () => true]

$.types(arr) // ['number', 'boolean', 'string', 'object', 'symbol', 'function']

/**
 * Can also be used to test types of non-arrays,
 * but not reccomended. Use typeof instead.
 */
$.types('foo') // 'string'