473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

is there anything like 'IN (val1,val2,val3)'

Javascript is new to me. Is there any way to express
if (var IN (val1, val2, val3, val4)) without using
something like
if (var == val1 && var == val2 && var == val3)
or looping thru an array?

TIA,
Marc Miller


Apr 20 '06 #1
5 1693

Marc Miller wrote:
Javascript is new to me. Is there any way to express
if (var IN (val1, val2, val3, val4)) without using
something like
if (var == val1 && var == val2 && var == val3)
or looping thru an array?


See the July 22, 2005 blog entry

http://trimpath.com/blog/

This might be what you want.

Peter

Apr 20 '06 #2
wrote:
Marc Miller wrote:
Javascript is new to me. Is there any way to express
if (var IN (val1, val2, val3, val4)) without using
something like
if (var == val1 && var == val2 && var == val3)
or looping thru an array?


See the July 22, 2005 blog entry

http://trimpath.com/blog/

This might be what you want.


Maybe, but you should also be aware, building on the example in the page
referenced by the blog, that all of the alerts below will display true:

function set ()
{
var result = {};

for (var i = 0; i < arguments.length; i++)
result[arguments[i]] = true;

return result;
}
alert(2 in set(2, 3, 4, 7, 8));
alert('2' in set(2, 3, 4, 7, 8));
alert('toString' in set(2, 3, 4, 7, 8));
Apr 20 '06 #3
pe**********@gmail.com wrote:
Marc Miller wrote:
Javascript is new to me. Is there any way to express
if (var IN (val1, val2, val3, val4)) without using
<nitpick>This can never work because `var' is a reserved word.</nitpick>
something like
if (var == val1 && var == val2 && var == val3)
or looping thru an array?


See the July 22, 2005 blog entry

http://trimpath.com/blog/

This might be what you want.


You could have posted a usable URL:

<URL:http://trimpath.com/blog/?m=200507>

or even the original one:

<URL:http://laurens.vd.oever.nl/weblog/items2005/setsinjavascript/>

However, this approach is not new (only its implementation could be),
and the solution introduces a dependency on the `in' operator, not
supported before JavaScript 1.4 (SSJS/Gecko), JScript 5.0 (IE 5.0),
ECMAScript Ed. 3, and, it is current form on Object literals, not
supported before JavaScript 1.3 (NN4), JScript 3.0 (IE 3.0), ECMAScript
Ed. 3 (that part can be either ignored, or fixed with `new Object').

It is one of the more elegant solutions, but it has a problem: because
with the `in' operator every first operand is converted to string (and
all property names are strings), one cannot distinguish between
differently typed values. For example,

2 in set("2", 3, 4)

yields true, although the "needle" is of type number, and the haystack
has only a value of type string.

Another solution that does not depend on those versions (and does not
have the type problem if strict equals is used if supported), would be
to define a function/method like the boolean operator:

function inArray(v, a)
{
for (var i = 0, len = a.length; i++)
{
if (v == a[i]) return true;
}

return false;
}

if (inArray(v, new Array(val1, val2, val3, val4))
{
// ...
}

I have implemented something similar in JSX:array.js. However, the Array
object is not needed, it is just there for distinguishing "needle"
(primitive value) and "haystack" (refers to the Array object encapsulating
the array data structure):

function inSet(v)
{
for (var i = 1, len = arguments.length; i++)
{
if (v == arguments[i]) return true;
}

return false;
}

if (inSet(v, val1, val2, val3, val4))
{
// ...
}

JavaScript 1.6 (Firefox 1.5+) also provides for a built-in solution[1] with
arrays and a closure:

function inArray(needle, aHaystack)
{
return aHaystack.some(
function(v, i, a)
{
return v === needle;
});
}

if (inArray(v, [val1, val2, val3, val4]))
{
// ...
}

With Array.prototype.forEach() and equivalents one could even count the
occurrences of v in the set/array. Since (±)0 evaluates to `false' and
any other number value except NaN to `true', that would be a worthwhile
extension, perhaps better be triggered by an additional optional argument
that defaults to `false'.
PointedEars
___________
[1] <URL:http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6>
Apr 20 '06 #4
Thomas 'PointedEars' Lahn wrote:
for (var i = 0, len = a.length; i++)
Either

for (var i = 0, len = a.length; i < len; i++)

or (with disregardment for element/argument order)

for (var i = a.length; i--;)
[...]
for (var i = 1, len = arguments.length; i++)


Similar here.
PointedEars
Apr 20 '06 #5
JRS: In article <uf******************@news1.epix.net>, dated Thu, 20
Apr 2006 16:01:30 remote, seen in news:comp.lang.javascript, Marc Miller
<mm*****@epix.net> posted :
Javascript is new to me. Is there any way to express
if (var IN (val1, val2, val3, val4)) without using
something like
if (var == val1 && var == val2 && var == val3)
or looping thru an array?

You can express it as
if (IsItIn(var, /**/ val1, val2, val3, val4))
by writing a function IsItIn.

The obvious implementation of IsItIn() involves looping through the
argument array.

You can also do

function IsItIn(Str, Obj) { return Obj[Str] }

IsItIn("val2", {val1:1, val2:2, val3:3, val4:4})

which returns either undefined or the value associated with val2. The
run-time code will be (probably?) looking in a hash table.
There may be other ways; presumably you could traverse a linked list of
your own construction, or seek in a has table likewise, ...
If you are thinking of the Pascal/Delphi SET type, you could implement
something similar for up to 32 elements by using integer operators on
Number-as-32-bit, or up to about 53 by using the mantissa of the IEEE
Double as such; and you can contrive arrays or lists of those.
Then arrays are sparse, so if your vars are non-negative integers you
can use an Array A as a set : all possible elements are initially
undefined, and an element N can be set to true to include N in the set
and false to exclude it. Then if (A[K]) does the test, with no
manifest looping. It seems that K can be any value from 0 to 2^53, and
any multiple of those below about 10^308; but check the ECMA spec.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Apr 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Hal Vaughan | last post by:
I am working on an install program, for another program that will need a list of printers on a system. Originally I found the printers by doing this: public PrintNames() { String tName = "";...
73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
4
by: Zheka | last post by:
we're dealing the following problem: d:\Program Files\Microsoft Visual Studio .NET\Vc7\include\vector(575): error C2440: 'initializing' : cannot convert from 'const Game' to 'Game' This...
9
by: Philip TAYLOR | last post by:
Configuring a new instance of IIS, I noticed that it allows an HTML-formatted document trailer to be appended to every document served. Unfortunately, on checking its behaviour, I find that it...
34
by: Justin Timberlake | last post by:
I was thinking about all those /Indian Outsourcing/ companies getting those .Net shops set up. 0. Nobody uses .NET in the real world, it's all java. 1. MSFT is about to collapse as witnessed by...
138
by: Ian Boyd | last post by:
i've been thrown into a pit with DB2 and have to start writing things such as tables, indexes, stored procedures, triggers, etc. The online reference is only so helpful. The two pdf manuals are...
20
by: Narf the Mouse | last post by:
....Without using NULL? There must be a way; people keep talking about storing pointers to objects in different locations in the program. Thanks.
28
by: hijkl | last post by:
hey guys anything wrong with this code?? if it is then what? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.