Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JavaScript Array every and some

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 }
    ]);
like image 373
STEEL Avatar asked Aug 04 '15 12:08

STEEL


People also ask

What is the difference between every and some?

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.

Why do we use the some () method in arrays?

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.

What is every () in JavaScript?

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.

What is difference between some and find?

The difference is in the output. find will return the value, some will return a boolean .

What is the difference between array some () and every () method in JavaScript?

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.

Is array any() exist in the JavaScript?

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.

What is the difference between every() and test() in JavaScript?

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.

What is the difference between map() and every() in JavaScript?

.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.


3 Answers

(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

like image 73
Royi Namir Avatar answered Oct 14 '22 17:10

Royi Namir


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
like image 37
Pavel Gatnar Avatar answered Oct 14 '22 17:10

Pavel Gatnar


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().

like image 27
Valentin Montmirail Avatar answered Oct 14 '22 18:10

Valentin Montmirail