@svizzle/utils/array-array

Methods

(static) getFirstAndLast(array) → {array}

Source:
Since:
  • 0.3.0

Return an array containing the first and the last element of the provided array.

Example
getFirstAndLast([0, 1, 2, 3, 4]) // [0, 4]
Parameters:
Name Type Description
array array
Returns:
Type
array

(static) inclusiveRange(args) → {array}

Source:
Since:
  • 0.7.0
See:

Return the range within the provided limits, both limits being included.

Example
> inclusiveRange([2, 5])
[2, 3, 4, 5]
> inclusiveRange([2, 12])
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
> inclusiveRange([2, 12, 2])
[2, 4, 6, 8, 10, 12]
> inclusiveRange([2, 11, 2])
[2, 4, 6, 8, 10];
Parameters:
Name Type Description
args array
Properties
Name Type Attributes Default Description
0 number

start

1 number

end

2 number <optional>
1

step

Returns:
Type
array

(static) makeBiPermutations(items) → {array}

Source:
Since:
  • 0.5.0

Return the permutations of pairs of the provided items.

Example
> array = [
	{foo: 'a'},
	{foo: 'b'},
	{bar: 'c'},
	{bar: 'd'}
]
> makeBiPermutations(array)
[
	[{foo: 'a'}, {foo: 'b'}],
	[{foo: 'a'}, {bar: 'c'}],
	[{foo: 'a'}, {bar: 'd'}],
	[{foo: 'b'}, {bar: 'c'}],
	[{foo: 'b'}, {bar: 'd'}],
	[{bar: 'c'}, {bar: 'd'}]
]
Parameters:
Name Type Description
items array
Returns:

permutations - permutations of pairs of items

Type
array

(static) pluckKey() → {Array.<string>}

Source:

Pluck the key property value from an array of objects.

Example
> pluckKey([{key: 'John', value: 'Foo'}, {key: 'Jane', value: 'Bar'}])
['John', 'Jane']
> pluckKey([{key: 'b', value: 2}, {key: 'a', value: 1}])
['b', 'a']
Parameters:
Type Description
Array.<object>

key/value items

Returns:

array of keys.

Type
Array.<string>

(static) pluckValue() → {Array.<string>}

Source:
Since:
  • 0.21.0

Pluck the value property value from an array of objects.

Example
> pluckValue([{key: 'John', value: 'Foo'}, {key: 'Jane', value: 'Bar'}])
['Foo', 'Bar']
> pluckValue([{key: 'b', value: 2}, {key: 'a', value: 1}])
[2, 1]
Parameters:
Type Description
Array.<object>

key/value items

Returns:

array of values.

Type
Array.<string>

(static) setIndexAsKey() → {Array.<object>}

Source:
Since:
  • 0.9.0

Return a copy of the provided array of objects assigning each object index to a property with the provided key (defaulting to index)

Example
> setIndex = setIndexAsKey()
> setIndex([{a: 2}, {c: 5}])
[{a: 2, index: 0}, {c: 5, index: 1}]

> setIndexAsIdx = setIndexAsKey('idx')
> setIndexAsIdx([{a: 2}, {c: 5}])
[{a: 2, idx: 0}, {c: 5, idx: 1}]
Parameters:
Type Description
Array.<object>

Array of objects

Returns:
  • Array of objects
Type
Array.<object>

(static) sortValueAscKeyAsc() → {array}

Source:
Since:
  • 0.5.0

Return a copy of the provided array with items sorted by value (ascending) then by key (ascending)

Example
> items = [
	{key: 'b', value: 1},
	{key: 'a', value: 4},
	{key: 'a', value: -30},
	{key: 'a', value: 1},
]
> sortValueAscKeyAsc(items)
[
	{key: 'a', value: -30},
	{key: 'a', value: 1},
	{key: 'b', value: 1},
	{key: 'a', value: 4},
]
Parameters:
Type Description
array

key/value items

Returns:
  • sorted items
Type
array

(static) sortValueAscKeyDesc() → {array}

Source:
Since:
  • 0.5.0

Return a copy of the provided array with items sorted by value (ascending) then by key (descending)

Example
> items = [
	{key: 'b', value: 1},
	{key: 'a', value: 4},
	{key: 'a', value: -30},
	{key: 'a', value: 1},
]
> sortValueAscKeyAsc(items)
[
	{key: 'a', value: -30},
	{key: 'b', value: 1},
	{key: 'a', value: 1},
	{key: 'a', value: 4},
]
Parameters:
Type Description
array

key/value items

Returns:
  • sorted items
Type
array

(static) sortValueDescKeyAsc() → {array}

Source:
Since:
  • 0.5.0

Return a copy of the provided array with items sorted by value (descending) then by key (ascending)

Example
> items = [
	{key: 'b', value: 1},
	{key: 'a', value: 4},
	{key: 'a', value: -30},
	{key: 'a', value: 1},
]
> sortValueDescKeyAsc(items)
[
	{key: 'a', value: 4},
	{key: 'a', value: 1},
	{key: 'b', value: 1},
	{key: 'a', value: -30},
]
Parameters:
Type Description
array

key/value items

Returns:
  • sorted items
Type
array

(static) sortValueDescKeyDesc() → {array}

Source:
Since:
  • 0.5.0

Return a copy of the provided array with items sorted by value (descending) then by key (descending)

Example
> items = [
	{key: 'b', value: 1},
	{key: 'a', value: 4},
	{key: 'a', value: -30},
	{key: 'a', value: 1},
]
> sortValueDescKeyDesc(items)
[
	{key: 'a', value: 4},
	{key: 'b', value: 1},
	{key: 'a', value: 1},
	{key: 'a', value: -30},
]
Parameters:
Type Description
array

key/value items

Returns:
  • sorted items
Type
array

(static) swap(array, index1, index2) → {array}

Source:
Since:
  • 0.1.0

Return a copy of the array with values at the provided indices swapped

Example
swap([0, 1, 2, 3, 4, 5], 1, 4) // [0, 4, 2, 3, 1, 5]
Parameters:
Name Type Description
array array
index1 integer
index2 integer
Returns:
Type
array

(static) toggleItem(array, item) → {array}

Source:
Since:
  • 0.1.0

Return a copy of the provided array without all instances of the provided item if the items is in the array or appending the item otherwise.

Example
> arr0 = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
> arrNo0 = [1, 2, 3, 4, 1, 2, 3, 4]
> arrTail0 = [1, 2, 3, 4, 1, 2, 3, 4, 0]
> arrTailObj = [1, 2, 3, 4, 1, 2, 3, 4, {a: 1}]

> toggleItem(arr0, 0)
[1, 2, 3, 4, 1, 2, 3, 4]
> toggleItem(arrNo0, 0)
[1, 2, 3, 4, 1, 2, 3, 4, 0]
> toggleItem(arrNo0, {a: 1})
[1, 2, 3, 4, 1, 2, 3, 4, {a: 1}]
> toggleItem(arrTailObj, {a: 1})
[1, 2, 3, 4, 1, 2, 3, 4]
Parameters:
Name Type Description
array array
item *
Returns:
Type
array