我看到在给定的测试中两者都返回 true 或 false。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
一起使用它们的正确情况应该是什么?
测试代码:
function checkUsersValid(goodUsers) {
return function allUsersValid(submittedUsers) {
//Im testing arrays here
return submittedUsers.every(function isBigEnough(element, index, array) {
return goodUsers.some(function (el, i, arr) {
return element.id == el.id;
});
});
};
}
var goodUsers = [
{ id: 1 },
{ id: 2 },
{ id: 3 }
];
var testAllValid = checkUsersValid(goodUsers);
testAllValid([
{ id: 2 },
{ id: 1 }
]);
(如果您了解 C# LINQ ,就像Any
vs All
)
其中谓词表示返回的函数bool
(真/假)对于每个元素
every
首先返回false
.
some
首先返回true
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)