I see both return true or false upon given tests.
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
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 }
]);
The main difference between the mentioned methods is that the some() method is used for finding at least one or more than one value in the array according to the passed condition, whereas the every() method checks whether all elements of an array are satisfying the given condition or not.
The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false.
The every() method executes a function for each array element. The every() method returns true if the function returns true for all elements. The every() method returns false if the function returns false for one element.
The difference is in the output. find will return the value, some will return a boolean .
The Array.some() method in JavaScript is used to check whether at least one of the elements of the array satisfies the given condition or not. The only difference is that the some() method will return true if any predicate is true while every() method will return true if all predicate are true. Example 1: This example implements the some() method.
Check the attached stackblitz project. Is Array.any () exist in the JavaScript? The answer is no. However, Array.some () do the purpose of getting any elements. The method Some will return true when the first matching condition will be met.
An important difference with .every () is that the test function may not always be called for every element in the array. Once the testing function returns false for any element, no more array elements are iterated. Therefore, the testing function should usually have no side effects.
.map () returns a new Array of objects created by taking some action on the original item. .every () returns a boolean - true if every element in this array satisfies the provided testing function. An important difference with .every () is that the test function may not always be called for every element in the array.
(If you know C# LINQ , it's like Any
vs All
)
some
will return true if any predicate is true
every
will return true if all predicate is true
Where predicate means function that returns bool
( true/false) for each element
every
returns on first false
.some
returns on first true
some
is analogue to logical or
every
is analogue to logical and
logically every
implies some
, but not in reverse
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
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 some
elements or every
elements.
If every() returns true
then some() returns true
.
but
If some() returns true
then we cannot conclude anything about the result of every()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With