JavaScript Array every 和 some 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31809682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between JavaScript Array every and some
提问by STEEL
I see both return true or false upon given tests.
我看到在给定的测试中都返回 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
What should be the right case to use them both together ?
将它们一起使用的正确情况应该是什么?
Test code:
测试代码:
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 }
]);
回答by Royi Namir
(If you know C# LINQ , it's like Anyvs All)
(如果你知道 C# LINQ ,它就像Anyvs All)
somewill return true if anypredicate istrueeverywill return true if allpredicate istrue
some如果有任何谓词,将返回 truetrueevery如果所有谓词都为真,则返回真true
Where predicate means function that returns bool( true/false) for each element
其中谓词表示bool为每个元素返回(真/假)的函数
everyreturns on first false.somereturns on first true
every首先返回false。some首先返回true
回答by Pavel Gatnar
someis analogue to logical oreveryis analogue to logical and
some类比于逻辑orevery类比于逻辑and
logically everyimplies some, but not in reverse
逻辑上every暗示some,但不是相反
try this:
试试这个:
var identity = function(x){return x}
console.log([true, true].some(identity))//true
console.log([true, true].every(identity))//true
console.log([true, false].some(identity))//true
console.log([true, false].every(identity))//false
console.log([false, false].some(identity))//false
console.log([false, false].every(identity))//false
console.log([undefined, true].some(identity))//true
console.log([undefined, true].every(identity))//false
console.log([undefined, false].some(identity))//false
console.log([undefined, false].every(identity))//false
console.log([undefined, undefined].some(identity))//false
console.log([undefined, undefined].every(identity))//false
回答by Valentin Montmirail
The documentation answers your question...
该文档回答了您的问题...
The some()method tests whether some element in the array passes the test implemented by the provided function.
The every()method tests whether all elements in the array pass the test implemented by the provided function.
在一些()方法测试所述阵列中的一些元件是否通过由提供的功能来实现的测试。
的每一个()方法测试是否阵列中的所有元件由通过所提供的功能来实现的测试。
So you will use them, according if you want to test someelements or everyelements.
因此,您将根据是否要测试some元素或every元素来使用它们。
If every() returns truethen some() returns true.
如果every() returns true那么some() returns true。
but
但
If some() returns truethen we cannot conclude anything about the result of every().
如果some() returns true那么we cannot conclude anything about the result of every()。
回答by axelduch
Array.prototype.someis good if you are looking for an intruder or a relic, meaning you only need to know if there is one at least that satisfies your constraints.
Array.prototype.some如果您正在寻找入侵者或遗物,这很好,这意味着您只需要知道是否至少有一个满足您的限制。
Array.prototy.everyon the other hand is useful to check the integrity of an array, for example, "Are all items of my array an instance of Car?".
Array.prototy.every另一方面,对于检查数组的完整性很有用,例如,“我的数组中的所有项目都是Car?的实例吗?”。
回答by Willem van der Veen
Summary:
概括:
- Use
Array.prototype.someto find out if one particular element in the array matches a boolean condition. - Use
Array.prototype.everyto find out if all elements in the array matches a boolean condition.
- 使用
Array.prototype.some找出如果阵列中的一个特定元素相匹配的布尔条件。 - 使用
Array.prototype.every找出如果阵列中的所有元素相匹配的布尔条件。
Example:
例子:
const array = [1, 2, 3, 4, 5];
const evenArr = [2, 4, 6];
const even = (element) => {
return element % 2 === 0;
};
console.log(array.some(even)); // true
console.log(array.every(even)) // false
console.log(evenArr.every(even)) // true
回答by Abhishek Gautam
Simple example of array.every():The every() method tests whether all elements in the array pass the test implemented by the provided function.
array.every() 的简单示例:every() 方法测试数组中的所有元素是否通过提供的函数实现的测试。
Note: This method returns true for any condition put on an empty array.
注意:对于放置在空数组上的任何条件,此方法返回 true。
function lessThanTwenty(currentValue) {
return currentValue < 20;
}
let array = [1, 30, 39, 29, 10, 19];
console.log(array.every(lessThanTwenty));
//output : true
Simple example of array.some():
array.some() 的简单示例:
The some() method tests whether at least one element in the array passes the test implemented by the provided function.
some() 方法测试数组中是否至少有一个元素通过了提供的函数实现的测试。
Note: This method returns false for any condition put on an empty array.
注意:对于放置在空数组上的任何条件,此方法返回 false。
let array2 = [1, 2, 3, 4, 5];
let even = function(ele) {
return ele % 2 === 0;
};
console.log(array.some(even));
// output: true

