The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Logical conditions as array values
Christian J
post May 23 2016, 12:34 PM
Post #1


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



Sorry if my language is confusing, I don't how to describe the problem better.

Anyway, I'd like to store a list of various logical conditions in an array, something like this:

CODE
var arr=[
x=='foo',
x>5
]

But this causes a script error since x does not yet exist. Is it possible to avoid the variables somehow?

The following works:

CODE
<script type="text/javascript">
var arr=[
function(x){return x=='foo';},
function(x){return x>5;}
]

if(arr[0]('foo'))
{
    alert('String value "foo" matches array element 0.');
}
if(arr[1](6))
{
    alert('Number value 6 matches array element 1.');
}
</script>

...but it seems convoluted to use a whole function in each array value. Can you do it in a simpler way?

Using regex as array values instead should work, but regex is not practical with numbers.
User is online!PM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 24th April 2024 - 05:04 PM