Namespace atropa.arrays
Version
20130221.
Utilities for handling arrays.
Defined in: <node_modules/atropa-arrays/src/atropa-arrays.js>.
Constructor Attributes | Constructor Name and Description |
---|---|
Utilities for handling arrays.
|
Method Attributes | Method Name and Description |
---|---|
<static> |
atropa.arrays.deleteElement(arr, index)
Deletes the given element from the array at the given index.
|
<static> |
atropa.arrays.getFrequency(arr)
Calculates the frequency of items occurring in an array.
|
<static> |
atropa.arrays.getUnique(largeArray)
Gets Unique values from an array.
|
<static> |
atropa.arrays.intersect(array1, array2)
Returns an array of values found in both of the given arrays.
|
<static> |
atropa.arrays.match(array1, array2)
Compares two arrays based on size, contents, and element order.
|
<static> |
atropa.arrays.reindex(arr)
Reindexes an array.
|
<static> |
atropa.arrays.removeEmptyElements(arrayWithEmptyElements)
Removes empty strings from the given array.
|
<static> |
atropa.arrays.sortAlphabetically(arr)
Throws an error,
String.prototype.localeCompare is not
standardized. |
<static> |
atropa.arrays.sortNumerically(arr)
Sorts an array's elements numerically.
|
<static> |
atropa.arrays.subtract(a, fromB)
Subtracts one array from another array based on the unique values in both
sets.
|
Method Detail
<static>
atropa.arrays.deleteElement(arr, index)
Deletes the given element from the array at the given index. It basically
does what you would expect the delete operator to do, except the delete
operator doesn't do what you would expect.
- Parameters:
- {Array} arr
- The array.
- {Number} index
- The index of the element to delete.
- Returns:
- Returns an array with the element removed, contiguous keys, and whose length is 1 less than the input array.
<static>
{Object}
atropa.arrays.getFrequency(arr)
Calculates the frequency of items occurring in an array.
Author: Matthew Christopher Kastor-Inare III
☭ Hial Atropa!! ☭.
Author: Matthew Christopher Kastor-Inare III
☭ Hial Atropa!! ☭.
var x = [1,1,1,1,1,3,3]; atropa.arrays.getFrequency(x); // returns { // "1": 5, // "3": 2 // }
var x = ["bill", "fred", "fred", "jane"]; atropa.arrays.getFrequency(x); // returns { // "bill": 1, // "fred": 2, // "jane": 1 // }
var x = [1,3,{'aProp' : 'aVal'}]; atropa.arrays.getFrequency(x); // returns { // "1": 1, // "3": 1, // "[object Object]": 1 // }
var obj = {'aProp' : 'aVal'}; var otherObj = {}; var x = [1,3,obj,otherObj,{'aDoughnut' : 'sprinkles'}]; atropa.arrays.getFrequency(x); // returns { // "1": 1, // "3": 1, // "[object Object]": 3 // }
var x = [1,3,"toString"]; atropa.arrays.getFrequency(x); // returns { // "1": 1, // "3": 1, // "toString": "function toString() {\n [native code]\n}1" // }
- Parameters:
- {Array} arr
- The array to calculate frequencies from.
- Returns:
- {Object} Returns an object whose keys are each unique elements from the array and their value is their frequency of occurrence within the array. Be careful that your array does not contain values matching object instance property names.
<static>
{Array}
atropa.arrays.getUnique(largeArray)
var x = [1,1,1,4,4,3,6]; atropa.arrays.getUnique(x); // returns [ "1", "4", "3", "6" ]
var x = ["bill", "fred", "jane", "fred"]; atropa.arrays.getUnique(x); // returns ["bill", "fred", "jane"]
var x = [ "bill", {"aProp" : "aValue"}, {"aGuy" : "fred"}, {"aLady" : "jane"} ]; atropa.arrays.getUnique(x); // returns [ "bill", "[object Object]" ]
- Parameters:
- {Array} largeArray
- The array with duplicate values in it.
- Returns:
- {Array} Returns a new array containing only the unique values found in the largeArray.
<static>
{Array}
atropa.arrays.intersect(array1, array2)
Returns an array of values found in both of the given arrays.
Author: Matthew Christopher Kastor-Inare III
☭ Hial Atropa!! ☭.
Author: Matthew Christopher Kastor-Inare III
☭ Hial Atropa!! ☭.
var x = [1,3,4]; var y = [3,1,5]; atropa.arrays.intersect(x,y); // returns [1,3]
var x = [1,1,3,4]; var y = [3,1,1,5]; atropa.arrays.intersect(x,y); // returns [1,1,3]
var obj = {'aProp' : 'aVal'}; var x = [1,3,obj]; var y = [3,1,obj]; atropa.arrays.intersect(x,y); // returns [1,3,{'aProp' : 'aVal'}]
var obj = {'aProp' : 'aVal'}; var x = [1,3,{'aProp' : 'aVal'}]; var y = [3,1,obj]; atropa.arrays.intersect(x,y); // returns [1,3] because the two objects are not the same object.
var x = [1,3,{'aProp' : 'aVal'}]; var y = [3,1,{'aProp' : 'aVal'}]; atropa.arrays.intersect(x,y); // returns [1,3] because the two objects are not the same object.
- Parameters:
- {Array} array1
- An array.
- {Array} array2
- Another array.
- Returns:
- {Array} Returns an array of values found in both of the given arrays.
<static>
{Boolean}
atropa.arrays.match(array1, array2)
Compares two arrays based on size, contents, and element order.
Author: Matthew Christopher Kastor-Inare III
☭ Hial Atropa!! ☭.
Author: Matthew Christopher Kastor-Inare III
☭ Hial Atropa!! ☭.
var x = [1,2]; var y = [1,1,3]; atropa.arrays.match(x,y); // returns false
var x = [1,2]; var y = [1,2]; atropa.arrays.match(x,y); // returns true
var x = [1,2]; var y = [2,1]; atropa.arrays.match(x,y); // returns false because the elements are not in the same order.
var x = [1,{'aProp' : 'aValue'}]; var y = [1,{'aProp' : 'aValue'}]; atropa.arrays.match(x,y); // returns false because even though the object looks the same, the // two objects are in fact distinct objects.
var obj = {'aProp' : 'aValue'}; var x = [1,obj]; var y = [1,obj]; atropa.arrays.match(x,y); // returns true because the objects referenced in the arrays are // in fact the same object.
- Parameters:
- {Array} array1
- One array you want compared to another.
- {Array} array2
- The other array.
- Returns:
- {Boolean} Returns true or false depending on whether or not the arrays matched in size, composition, and element order.
<static>
{Array}
atropa.arrays.reindex(arr)
var x = [ "a", "b", "c", undefined ]; console.log(x); // [ "a", "b", "c", undefined ] console.log(x.length); // 4 delete x[1]; // deletes the key from the array but // the array length remains the same // at this point the arrays keys are 0, 2, and 3 console.log(x); // [ "a", undefined, "c", undefined ] console.log(x.length); // 4 x = atropa.arrays.reindex(x); console.log(x); // [ "a", "c", undefined ] // note that the last element existed in the array, its value was // undefined but it did have a key so the element remains in the array. // // The deleted element was in fact deleted from the array so there was no // key x[1] at all, when trying to access this non existing element the // value of undefined was returned. This behavior is confusing unless you // think about the arrayas an object whose properties are named by // numbers. Accessing an undefined property returns undefined regardless // of whether the property existed in the past or not. console.log(x.length); // 3
- Parameters:
- {Array} arr
- The array with discontinuous keys.
- Returns:
- {Array} Returns an array with continuous keys.
<static>
{Array}
atropa.arrays.removeEmptyElements(arrayWithEmptyElements)
Removes empty strings from the given array.
Author: Matthew Christopher Kastor-Inare III
☭ Hial Atropa!! ☭.
Author: Matthew Christopher Kastor-Inare III
☭ Hial Atropa!! ☭.
var x = [ 10, , 5, "", '', 7 ]; console.log('starting length ' + x.length); console.log(x); x = atropa.arrays.removeEmptyElements(x); console.log('ending length ' + x.length); console.log(x); // displays the following // starting length 6 // [10, undefined, 5, "", "", 7] // ending length 3 // [10, 5, 7]
- Parameters:
- {Array} arrayWithEmptyElements
- The array with empty strings in it.
- Returns:
- {Array} Returns a new array with empty strings removed.
<static>
atropa.arrays.sortAlphabetically(arr)
Throws an error,
String.prototype.localeCompare
is not
standardized.
Yes, localeCompare is in the standard but, at this time the actual
comparison is implementation dependant. This means that "alphabetical order"
can be different on different platforms. What I found was that in node the
array of ['a','Z','A','z']
would be sorted to
['A','Z','a','z"]
, while on
firefox it would be sorted to ['a','A','z','Z']
. Who knows if
another implementor would sort it ['A','a','Z','z']
?
In order to provide a reliable implementation I would have to create my own
implementation of String.prototype.localeCompare
and that's
just too much work for me to do alone.
- Parameters:
- arr
- Throws:
- {Error}
- "String.prototype.localeCompare is not standardized"
<static>
{Array}
atropa.arrays.sortNumerically(arr)
Sorts an array's elements numerically.
Author: Matthew Christopher Kastor-Inare III
☭ Hial Atropa!! ☭.
Author: Matthew Christopher Kastor-Inare III
☭ Hial Atropa!! ☭.
var x = [3, 2, 9, 26, 10, 1, 99, 15]; console.log( atropa.arrays.sortNumerically(x) ); // logs [1, 2, 3, 9, 10, 15, 26, 99]
- Parameters:
- {Array} arr
- The array to sort. All elements of the array must be number-ish.
- Returns:
- {Array} Returns an array whose elements are in numeric order.
<static>
{Array}
atropa.arrays.subtract(a, fromB)
Subtracts one array from another array based on the unique values in both
sets.
Author: Matthew Christopher Kastor-Inare III
☭ Hial Atropa!! ☭.
Author: Matthew Christopher Kastor-Inare III
☭ Hial Atropa!! ☭.
var x = [1,2]; var y = [1,1,3]; atropa.arrays.subtract(x,y); // returns [3]
var x = [1,3]; var y = [3,1]; atropa.arrays.subtract(x,y); // returns []
var x = [1,3]; var y = [3,1,1,9]; atropa.arrays.subtract(x,y); // returns [9]
var x = [1,3,{'aProp' : 'aVal'}]; var y = [3,1,{'aProp' : 'aVal'}]; atropa.arrays.subtract(x,y); // returns [{'aProp' : 'aVal'}] // because the two objects are not the same object.
var obj = {'aProp' : 'aVal'}; var x = [1,3,obj]; var y = [3,1,{'aProp' : 'aVal'}]; atropa.arrays.subtract(x,y); // returns [{'aProp' : 'aVal'}] // because the two objects are not the same object.
var obj = {'aProp' : 'aVal'} var x = [1,3,obj]; var y = [3,1,obj]; atropa.arrays.subtract(x,y); // returns [] // because the objects referenced in the arrays are the same object.
- Parameters:
- {Array} a
- (subtrahend) The array to subtract.
- {Array} fromB
- (minuend) The array with elements duplicated in
a
- Returns:
- {Array} Returns a new array containing only the unique
values found in
fromB
that are not present ina