扩展Array方法
Array.prototype.where = function (callback) {
var array = []
for (var i = 0; i < this.length; i++) {
if (callback.apply(this[i]) != false) {
array.push(this[i])
}
}
return array;
}
Array.prototype.each = function (callback) {
for (var i = 0; i < this.length; i++) {
if (callback.apply(this[i]) == false) {
break;
}
}
}
var data = [];
data.push({ min: 0, max: 100, value: 1 });
data.push({ min: 51, max: 150, value: 2 });
data.push({ min: 101, max: 200, value: 3 });
data.push({ min: 151, max: 450, value: 4 });
var N = 150;
data.where(function () {
return (this.min <= N && this.max >= N)
}).each(function () {
alert(this.value);
});
var array = []
for (var i = 0; i < this.length; i++) {
if (callback.apply(this[i]) != false) {
array.push(this[i])
}
}
return array;
}
Array.prototype.each = function (callback) {
for (var i = 0; i < this.length; i++) {
if (callback.apply(this[i]) == false) {
break;
}
}
}
var data = [];
data.push({ min: 0, max: 100, value: 1 });
data.push({ min: 51, max: 150, value: 2 });
data.push({ min: 101, max: 200, value: 3 });
data.push({ min: 151, max: 450, value: 4 });
var N = 150;
data.where(function () {
return (this.min <= N && this.max >= N)
}).each(function () {
alert(this.value);
});