Skip to content

Instantly share code, notes, and snippets.

@Evghenusi
Forked from 140bytes/LICENSE.txt
Created February 17, 2012 12:08
Show Gist options
  • 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

@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