How to create a contains function using shorthand instead of a for loop in JavaScript?

I think these are two questions. I'm still having problems with the reduction method, I get an easy way to use it.

reduce([1,2,3], function(a, b) { return a + b; }, 0); //6

Using this with anything other than numbers really bothers me. So, how do I build a contains function using shorthand instead of a for loop? Comments will be appreciated. Thanks to everyone.

 function contains(collection, target) { for(var i=0; i < collection.length; i++){ if(collection[i] === target){ return true; } } return false; } contains([1, 2, 3, 4, 5], 4); //true 
+6
source share
3 answers

That's what you need:

 function contains(collection, target) { return collection.reduce( function(acc, elem) { return acc || elem == target; }, false) }; 

As Adaneo says, there is probably an easier way for this particular question, but you have noted this as “functional programming”, so I think you want to improve this way of solving problems that I fully approve of.

+2
source

Here is the ES2015 solution:

  const contains = (x, xs) => xs.some(y => x === y); let collection = [1,2,3,4,5]; console.log(contains(4, collection)); // true; 

The great advantage of Array.prototype.some over Array.prototype.reduce is that the former completes the iteration as soon as the condition is true , while the latter always passes through the entire array. This means that contains(4, xs) stops the iteration with the fourth element xs .

+2
source

How can I make X using Y?

In general, I approach all of these issues the same way: programming languages ​​are not designed for magic wands. If your language does not have a built-in function or behavior, you can write it yourself. From there, if you later find out that your language offers a built-in language for this behavior (or it is added), you can reorganize your code if you want. But by all means, don't sit around waiting for the wand to wave and your code to work magically.

You can use Array.prototype.reduce if you want, but given how it works, it will always Array.prototype.reduce over the entire contents of the array - even if a match is found in the first element. Thus, this means that you should not use Array.prototype.reduce for your function.

However, you can use reduction if you write reduce , which supports early exit. Below is reducek , which passes the continuation to the callback. Applying a continuation will continue, but returning the value will result in an early termination. It seems that exactly what the doctor ordered ...

This answer is intended to accompany LUH3417's answer, to show you that before you can be aware of Array.prototype.some , you should not sit around waiting for ECMAScript to execute your behavior. This answer demonstrates that you can use the reduction procedure and still have early exit behavior.

 const reducek = f=> y=> ([x,...xs])=> x === undefined ? y : f (y) (x) (y=> reducek (f) (y) (xs)) const contains = x=> reducek (b=> y=> k=> y === x ? true : k(b)) (false) console.log(contains (4) ([1,2,3,4,5])) // true console.log(contains (4) ([1,2,3,5])) // false console.log(contains (4) ([])) // false 

Seeing reducek here and an example of the contains function, it should be somewhat obvious that contains could be generalized, which is Array.prototype.some .

Again, programming is not magic, so I will show you how you could do it if Array.prototype.some does not exist yet.

 const reducek = f=> y=> ([x,...xs])=> x === undefined ? y : f (y) (x) (y=> reducek (f) (y) (xs)) const some = f=> reducek (b=> x=> k=> f(x) ? true : k(b)) (false) const contains = x=> some (y=> y === x) console.log(contains (4) ([1,2,3,4,5])) // true console.log(contains (4) ([1,2,3,5])) // false console.log(contains (4) ([])) // false 
+2
source

All Articles