Skip to content

Instantly share code, notes, and snippets.

@dsetzer
Last active September 25, 2022 05:15
Show Gist options
  • Save dsetzer/e317372b5bcdd2310fe86e760f67c021 to your computer and use it in GitHub Desktop.
Save dsetzer/e317372b5bcdd2310fe86e760f67c021 to your computer and use it in GitHub Desktop.
A set of Array utils which I use (It should be noted that some are dependent on others)
// Calculates the sum of the items in the array.
// The sum is the result of adding all the items together.
Array.prototype.sum = function(){
return this.reduce((a, b) => a + b);
}
// Checks whether the specified item is present in the array.
Array.prototype.contains = function (item) {
return this.find(x => x === item) !== undefined;
}
// Counts the number of times the specified item occurs in the array.
Array.prototype.count = function(item){
return this.filter(x => x === item).length;
}
// Calculates the minimum value in the array.
// The minimum value is the smallest item in the array.
Array.prototype.min = function () {
return Math.min.apply(null, this);
}
// Calculates the maximum value in the array.
// The maximum value is the largest item in the array.
Array.prototype.max = function () {
return Math.max.apply(null, this);
}
// Calculates the statistical range of the array.
// The range is the difference between the largest and smallest values in the array.
Array.prototype.range = function(){
return this.max() - this.min();
}
// Calculates the arithmetic mean of the array.
// The arithmetic mean is the average of the items in the array.
Array.prototype.mean = function(){
return this.sum() / this.length
}
// Calculates the median for the specified percentile of the array.
Array.prototype.quartile = function (q) {
let a = this.slice().sort((c, d) => c - d);
let p = q * (a.length - 1), b = Math.floor(p);
return a[b + 1] ? a[b] + (p - b) * (a[b + 1] - a[b]) : a[b];
};
// Calculates the median of the array.
// The median is the middle item in a sorted array.
Array.prototype.median = function(){
return this.quartile(0.5);
}
// Calculates lower quartile median of the array.
// The lower quartile is the median of the lower half of the array.
Array.prototype.Q1 = function() {
return this.quartile(0.25);
}
// Calculates upper quartile median of the array.
// The upper quartile is the median of the higher half of the array.
Array.prototype.Q3 = function() {
return this.quartile(0.75);
}
// Calculates the interquartile range of the array.
// The interquartile range is the difference between the upper and lower quartiles.
Array.prototype.IQR = function(){
return this.Q3() - this.Q1();
}
// Calculates the statistical mid-range of the array.
// The mid-range is the mean of the lower quartile and the upper quartile.
Array.prototype.midRange = function(){
return (this.Q1() + this.Q3()) / 2;
}
// Calculates the lower outlier limit of the array.
Array.prototype.outlierMin = function(){
return this.Q1() - (1.5 * this.IQR());
}
// Calculates the upper outlier threshold of the array.
Array.prototype.outlierMax = function(){
return this.Q3() + (1.5 * this.IQR());
}
// Calculates the mode of the array.
// The mode is the most common item in the array.
Array.prototype.mode = function(){
return this.reduce((a, b) => (a.count(b) > b.count(b) ? a : b));
}
// Calculates the variance of the array.
// The variance is the sum of the squared deviations from the mean.
Array.prototype.variance = function(){
let mean = this.mean();
return this.map((num) => Math.pow(num - mean, 2)).mean();
}
// Calculates the standard deviation of the array.
// The standard deviation is the square root of the variance.
Array.prototype.stdDev = function(){
return Math.sqrt(this.variance());
}
// Calculates the absolute deviation of the array.
// The absolute deviation is the difference between the item and the mean.
Array.prototype.absDev = function(){
let mean = this.mean();
return this.map(num => Math.abs(num - mean)).mean();
}
// Calculates the z-scores for the items in the array.
// The z-score is the difference between the item and the mean divided by the standard deviation.
Array.prototype.zScore = function(){
let mean = this.mean();
let stdev = this.stdDev();
return this.map((num) => { return (num - mean) / stdev; });
}
const prototypes = new Map();
prototypes.set('count', function (item) { return this.filter(x => x === item).length; });
prototypes.set('contains', function (item) { return this.find(x => x === item) !== undefined; });
prototypes.set('quartile', function (q) {
let a = this.slice().sort((c, d) => c - d), p = q * (a.length - 1), b = Math.floor(p);
return a[b + 1] ? a[b] + (p - b) * (a[b + 1] - a[b]) : a[b];
});
prototypes.set('sum', function () { return this.reduce((a, b) => a + b); });
prototypes.set('min', function () { return Math.min.apply(null, this); });
prototypes.set('max', function () { return Math.max.apply(null, this); });
prototypes.set('range', function () { return this.max() - this.min(); });
prototypes.set('mean', function () { return this.sum() / this.length });
prototypes.set('median', function () { return this.quartile(0.5); });
prototypes.set('mode', function () { return this.reduce((a, b) => (a.count(b) > b.count(b) ? a : b)); });
prototypes.set('Q1', function () { return this.quartile(0.25); });
prototypes.set('Q3', function () { return this.quartile(0.75); });
prototypes.set('IQR', function () { return this.Q3() - this.Q1(); });
prototypes.set('midRange', function () { return (this.Q1() + this.Q3()) / 2; });
prototypes.set('outlierMin', function () { return this.Q1() - (1.5 * this.IQR()); });
prototypes.set('outlierMax', function () { return this.Q3() + (1.5 * this.IQR()); });
prototypes.set('variance', function () { return this.map((num) => Math.pow(num - this.mean(), 2)).mean(); });
prototypes.set('stdDev', function () { return Math.sqrt(this.variance()); });
prototypes.set('absDev', function () { return this.map(num => Math.abs(num - this.mean())).mean(); });
prototypes.set('zScore', function () { return this.map((num) => { return (num - this.mean()) / this.stdDev(); }); });
Array.from(prototypes).forEach(([key, value]) => {
Object.defineProperty(Array.prototype, key, {
get: function () { return value.bind(this)(); }
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment