Create a gist now

Instantly share code, notes, and snippets.

inArray

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

Array.prototype.inArray = function (v)
{
return (this.indexOf ? this : (v = ',' + v + ',', ',' + this + ',')).indexOf(v) >= 0
}
Array.prototype.inArray=function(v){return(this.indexOf?this:(v=','+v+',',','+this+',')).indexOf(v)>=0}
Array.prototype.inArray=function(a,c){for(c in this)if(this[c]===a)return!0;return!1}
// author @williammalo
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 Evghenusi
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "inArray",
"description": "Checks if a value exists in an array",
"keywords": [
"array"
]
}
<!DOCTYPE html>
<title>Foo</title>
<script>
Array.prototype.inArray=function(v){return(this.indexOf?this:(v=','+v+',',','+this+',')).indexOf(v)>=0}
var arr = [1,2,3,5,6,7];
alert(arr.inArray(8));
alert(arr.inArray(6));
</script>
@maettig
maettig commented Feb 17, 2012

Using return(this.indexOf?this:this.join()).indexOf(v)>=0 it's the same length but easier to understand. And, in older browsers it returns true if you are looking for "b" or "cx" in the array ["abc", "xyz"]. Not sure if this is an issues since all current browsers support Array.indexOf. You can fix this with something like ('\0'+this.join('\0')+'\0').indexOf('\0'+v+'\0').

@Evghenusi
Owner

Thank you very much, I think about it

@atk
atk commented Feb 29, 2012

How about Array.prototype.inArray=function(v,t,l){for(t=this,l=t.length;t[--l]!==v&&l>-2;);return-1<l}?

@Evghenusi
Owner

interesting solution

@atk
atk commented Feb 29, 2012

One could probably even save another byte by replacing l>-2 with l+2

@Evghenusi
Owner

@atk, I added your version, and removed one variable, I hope you will not be against

@atk
atk commented Feb 29, 2012

Not at all ;-)

@williammalo

Wait... can't you just do that?

function(b){return!!~this.indexOf(b)}

@Evghenusi
Owner

@williammalo, your version does not work in IE6/7 (in IE8 not tested)

@williammalo

@Evghenusi
You can use a indexOf polyfill, or this:
function(a,b,c,d){for(c in this)if(this[c]===a)d=!0;return d||!1}

@Evghenusi
Owner

function(a,c,d){for(c in this)if(this[c]===a)d=1;return!!d} -6 byte

@Evghenusi
Owner

function(a,c){for(i=d=0;c=this[i++];d+=c===a);return!!d} - another -3 bytes

@williammalo

@Evghenusi
I like the first one! I'll update my fork! Thanks!
The second one leaks i and d to the global scope, and declaring them would add 4 bytes

@Evghenusi
Owner

@williammalo, exactly, I forgot.

@maettig
maettig commented Mar 25, 2012

Note that a for-in loop does not loop an array as you would expect. It also returns the .length property. Due to this it returns true if you are looking for lets say "3" in every array of that length.

The last example returns wrong results if an array contains a "0". This coerces to false and the loop stops.

@williammalo

@maettig
I would have thought the same thing about .length, but I tested it (in safari and firefox) and I don't get false positives if I am looking for a number that is also the array's length.

Did you test it? And if so, in what browser?

@Evghenusi
Owner

@maettig

The last example returns wrong results if an array contains a "0". This coerces to false and the loop stops.
thanks, missed

@maettig
maettig commented Mar 25, 2012

You are right about .length. I always have this problem when iterating a NodeList with a for-in loop. But this does not happen with an Array. I find this very confusing. Shouldn't all objects that implement the Iterable interface behave the same? I guess the reason is, there is no Iterable interface in JavaScript.

@atk
atk commented Mar 26, 2012

Save variables: function(a,c){for(c in this)if(this[c]===a)return!0;return!1}

@williammalo

@atk

I think that will always return false no matter what.

It actually works!

@atk
atk commented Mar 26, 2012

x=function(a,c){for(c in this)if(this[c]===a)return!0;return!1} x.call([1,2,3,4],2) // -> true

nope, it doesn't. However, it could fail if you compare to some prototypical feature, like inArray (if you set it as prototype). To remedy for this, either use a numeric iteration or the hasOwnProperty-method; the latter has the advantage of being useful for Objects, too.

@Evghenusi
Owner

function(a,b,c,d){for(;this[b=-~b]!=d;c=c||this[b]===a);return c}
4||6 bytes larger and probably slower))))

@atk
atk commented Mar 26, 2012

Will fail on sparse arrays, e.g. [1,,3].

@Evghenusi
Owner

persuaded, I give up

@atk
atk commented Mar 27, 2012

You could still use function(a,b,c){for(;(b=-~b)in this;c=c||this[b]===a);return c}, but it's 2 bytes longer than my previous solution; but it doesn't suffer from inclusion of prototypic properties and is therefore the safest solution yet.

@williammalo

@atk
your function doesn't work if the number you test for is the first entry in the array
proof:
x=function(a,b,c){for(;(b=-~b)in this;c=c||this[b]===a);return c}​
document.write(x.call([2,3,4,5],2))​

@williammalo

@atk
quick fix in 4 bytes:
function(a,b,c){for(b=-1;(b=-~b)in this;c=c||this[b]===a);return c}

@Evghenusi
Owner

@williammalo, function(a,b,c){for(b=0;(b++)in this;c=c||this[b]===a);return c} ?

@williammalo

@Evghenusi
it still has the same problem
proof:
x=function(a,b,c){for(b=0;(b++)in this;c=c||this[b]===a);return c}
document.write(x.call([2,3,4,5],2))
//returns false

@Evghenusi
Owner

amusingly.


http://alokmenghrajani.github.com/tron/ 219 bytes tron

@maettig
maettig commented Mar 27, 2012

What about this? 62 bytes. Seems to work in all cases except for sparse arrays.

function(a,b,c){for(b=-1;++b in this;c|=this[b]===a);return c}
@williammalo

@maettig
Hawt stuff!
make it return boleans with 1 byte more:
function(a,b,c){for(b=-1;++b in this;c|=this[b]===a);return!!c}
and if you want to take the risk of using for-in:
function(a,b,c){for(b in this)c|=this[b]===a;return!!c}

@atk
atk commented Mar 27, 2012

easy: function(a,b,c){for(b=0;b in this;c=c||this[b++]===a);return c}​

@williammalo

@atk
It's funny, my edit of maettig's function is the exact same length as yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment