Skip to content

Instantly share code, notes, and snippets.

@Evghenusi
Forked from 140bytes/LICENSE.txt
Created February 17, 2012 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Evghenusi/1852990 to your computer and use it in GitHub Desktop.
Save Evghenusi/1852990 to your computer and use it in GitHub Desktop.
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>
@Evghenusi
Copy link
Author

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

@atk
Copy link

atk commented Feb 29, 2012

Not at all ;-)

@williammalo
Copy link

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

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

@Evghenusi
Copy link
Author

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

@williammalo
Copy link

@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
Copy link
Author

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

@Evghenusi
Copy link
Author

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

@williammalo
Copy link

@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
Copy link
Author

@williammalo, exactly, I forgot.

@maettig
Copy link

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
Copy link

@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
Copy link
Author

@maettig

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

@maettig
Copy link

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
Copy link

atk commented Mar 26, 2012

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

@williammalo
Copy link

@atk

I think that will always return false no matter what.

It actually works!

@atk
Copy link

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
Copy link
Author

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
Copy link

atk commented Mar 26, 2012

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

@Evghenusi
Copy link
Author

persuaded, I give up

@atk
Copy link

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.

@Evghenusi
Copy link
Author

@williammalo
Copy link

@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
Copy link

@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
Copy link
Author

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

@williammalo
Copy link

@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
Copy link
Author

amusingly.


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

@maettig
Copy link

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
Copy link

@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
Copy link

atk commented Mar 27, 2012

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

@williammalo
Copy link

@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