JavaScript does not have built in >
or <
operators for Arrays. But you can define them yourself for Arrays.
Greater than
// attach the .greaterThan method to Array's prototype to call it on any array
Array.prototype.greaterThan = function (array) {
// if the other array is a false value, return
if (!array)
return false;
var minArrayLength = Math.min(this.length, array.length);
for (var i = 0; i < minArrayLength; i++) {
if (this[i] > array[i]){
return true;
}
}
return false;
};
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "greaterThan", {enumerable: false});
Usage
[1, 2, 3, 4].greaterThan([1, 2, 3, 6]) === false;
[1, 13, 14, 11, 7].greaterThan( [1, 3, 14, 12, 11]) === true;
Less Than
// attach the .lessThan method to Array's prototype to call it on any array
Array.prototype.lessThan = function (array) {
// if the other array is a false value, return
if (!array)
return false;
var minArrayLength = Math.min(this.length, array.length);
for (var i = 0; i < minArrayLength; i++) {
if (this[i] > array[i]){
return false;
}
}
return true;
};
// Hide method from for-in loops
Object.defineProperty(Array.prototype, "lessThan", {enumerable: false});
Usage
[1, 2, 3, 2].lessThan([1, 2, 3, 4]) === true;
[1, 13, 14, 11, 7].lessThan( [1, 3, 14, 12, 11]) === false;