DEV Community

Rajnish Katharotiya
Rajnish Katharotiya

Posted on

Check if all records are matched with your prediction in javascript

Hi welcome folks, Javascript is one of the popular languages you can learn ( easily ). Herewith the series called "Javascript Useful Snippets ", I'm going to share some shortcodes and useful javascript methods. These snippets can help you to make your development more efficient and faster. So, stay tuned till the end...

Javascript Useful Snippets - all()

In order to check whether every value of your records/array is matching with your prediction or not, you can use this function. all() function returns true if the predicate function returns true for all elements in a collection and false otherwise. let's look at the syntax...

   const all = (arr, fn = Boolean) => arr.every(fn); 

Here, are passing two parameters - one is an array of records and the second is prediction function where you can omit the second argument if you want to use Booleanas a default value.

Result one :

   all([ 4, 3, 5], x => x > 1);   // output:  true 

Result Two :

   all([ 4, 1, 5], x => x > 1);   // output:  false 

Result three :

   all([ 4, 1, 5]);   // output:  true 

As you have seen in results above with prediction function it'll return as per values in an array which passed through parameters. and without prediction, it'll return default boolean value.

Now, what if we want to check whether all records of the array are equal or not? Well our next episode it all about it so, stay tuned and keep support me.

Thank you for watching folks, if you found this informative and wanted to make me more content like this please support me on Patreon.

Subscribe on youtube



Top comments (0)