如何在Javascript中编写Array.prototype.every()方法
How to write the Array.prototype.every() method in Javascript
作为练习,我想编写一个与 Array.prototype.every() 方法类似的函数 all()。此函数 returns 仅当谓词为数组中的所有项提供 returns true 时才为真。
Array.prototype.all = function (p) {
this.forEach(function (elem) {
if (!p(elem))
return false;
});
return true;
};
function isGreaterThanZero (num) {
return num > 0;
}
console.log([-1, 0, 2].all(isGreaterThanZero)); // should return false because -1 and 0 are not greater than 0
不知何故这不起作用,returns true
。我的代码有什么问题?有没有更好的写法?
您无法通过 returning 跳出 Array#forEach 循环。请改用 for 循环。
注意:这是 Array#every 的部分实现,用于演示 return 问题。
Array.prototype.all = function (p) {
for(var i = 0; i < this.length; i++) {
if(!p(this[i])) {
return false;
}
}
return true;
};
function isGreaterThanZero (num) {
return num > 0;
}
console.log([-1, 0, 2].all(isGreaterThanZero));
foreach 函数中的 return 不是函数中的 returning 值。你可以这样编码。
Array.prototype.all = function (p) {
for(var i = 0; i < this.length; i++){
if (!p(this[i]))
return false;
}
return true;
};
function isGreaterThanZero (num) {
return num > 0;
}
var result = [2, 3, 4].all(isGreaterThanZero);
console.log("result", result);
其他答案略有错误。您传入的回调应该使用 3 个参数调用:当前项、索引和整个数组。这就是本机 Array.every
以及大多数本机数组函数的工作方式。您的回调可以选择使用这些参数,但大多数时候它不会。
Array.prototype.all = function (p) {
for(var i = 0; i < this.length; i++) {
if (!p(this[i], i, this)) {
return false;
}
}
return true;
};
function isGreaterThanZero (num) {
return num > 0;
}
console.log([-1, 0, 2].all(isGreaterThanZero)); // should return false because -1 and 0 are not greater than 0
您只能通过抛出异常来停止 forEach()
循环。只需使用普通的 for
循环即可。
Array.prototype.all = function (p) {
for(var i = 0; i < this.length; i++) {
if(!p(this[i])) {
return false;
}
}
return true;
};
如果允许使用除Array.prototype.every()
以外的任何方法,那么您可以:
Array.prototype.all = function (p) {
return this.filter(p).length == this.length;
};
作为练习,我想编写一个与 Array.prototype.every() 方法类似的函数 all()。此函数 returns 仅当谓词为数组中的所有项提供 returns true 时才为真。
Array.prototype.all = function (p) {
this.forEach(function (elem) {
if (!p(elem))
return false;
});
return true;
};
function isGreaterThanZero (num) {
return num > 0;
}
console.log([-1, 0, 2].all(isGreaterThanZero)); // should return false because -1 and 0 are not greater than 0
不知何故这不起作用,returns true
。我的代码有什么问题?有没有更好的写法?
您无法通过 returning 跳出 Array#forEach 循环。请改用 for 循环。
注意:这是 Array#every 的部分实现,用于演示 return 问题。
Array.prototype.all = function (p) {
for(var i = 0; i < this.length; i++) {
if(!p(this[i])) {
return false;
}
}
return true;
};
function isGreaterThanZero (num) {
return num > 0;
}
console.log([-1, 0, 2].all(isGreaterThanZero));
return 不是函数中的 returning 值。你可以这样编码。
Array.prototype.all = function (p) {
for(var i = 0; i < this.length; i++){
if (!p(this[i]))
return false;
}
return true;
};
function isGreaterThanZero (num) {
return num > 0;
}
var result = [2, 3, 4].all(isGreaterThanZero);
console.log("result", result);
其他答案略有错误。您传入的回调应该使用 3 个参数调用:当前项、索引和整个数组。这就是本机 Array.every
以及大多数本机数组函数的工作方式。您的回调可以选择使用这些参数,但大多数时候它不会。
Array.prototype.all = function (p) {
for(var i = 0; i < this.length; i++) {
if (!p(this[i], i, this)) {
return false;
}
}
return true;
};
function isGreaterThanZero (num) {
return num > 0;
}
console.log([-1, 0, 2].all(isGreaterThanZero)); // should return false because -1 and 0 are not greater than 0
您只能通过抛出异常来停止 forEach()
循环。只需使用普通的 for
循环即可。
Array.prototype.all = function (p) {
for(var i = 0; i < this.length; i++) {
if(!p(this[i])) {
return false;
}
}
return true;
};
如果允许使用除Array.prototype.every()
以外的任何方法,那么您可以:
Array.prototype.all = function (p) {
return this.filter(p).length == this.length;
};