String in method - Array includes method

We have enjoyed the clarity of includes for array, like this:

var fruits = [banana, cherry, apple, arape];
if (fruits.includes("banana"))
...

But, when we need to test a group, i propose to use in that way:

var myStringFruit = "banana";
if (myStringFruit.in(fruits))
...

if (myStringFruit.in(["apple", "grape", "peach", "pomelo"]))
...

this is a more elegant solution of the traditional
if (myStringFruit=="apple" || myStringFruit=="grape" || myStringFruit=="peach".....

¿what you think?

1 Like

Why would we need myStringFruit.in(fruits) when we have fruits.includes(myStringFruit)? That seems like it'd be adding an extra thing to the language (a very expensive proposition) solely so "collection includes item" could be written in reverse, as "item is in collection".

1 Like

Yes friend, you right, but, for clarify effects, the method "in" in the String class would be nice,

["banana", "apple", "cherry"].includes(myStringFruit)

can be more clarify in this too:

myStringFruit.in(["banana", "apple", "cherry"]);

1 Like

I don't think an in method provides more clarity than the reverse. In addition, it seems to be on the wrong side - checking for existence is the responsibility of the collection, not of any arbitrary values:

  • value.in(collection) should work for a collection that is an array, a set, an iterator, etc. It would need to dispatch to the collection-specific method anyway (using a Symbol approach, similar to String's split and match methods supporting arbitrary patterns)
  • value.in(collection) should work for arbitrary values, including null, undefined and Object.create(null), which JavaScript is not designed to support. It seems you are proposing an in method only for strings?!

seems right bergus, i only want to clarify like sql sintax, but I agree that it can be solved in reverse.
in sql familiar sintax clarify
...
WHERE fruit IN ( ..... a list of elements)
I found it interesting to propose it
a single element (string) is in a group (Array or Set or Map)
Can be usefull with Numbers too looks this

var myNumer = 123;
if myNumer==11 || myNumer==22 || myNumber==54 || myNumber==55....

this can be solved simplifyg in this form:

myNumber.in(11,22,54,55)

avoiding wrong writing (the variable is writed only once)