Go to main content

exist

Check for a key in an hash array

EXIST

NAME

exist - Check if an element exist in a context.

SYNOPSIS

exist (context, element[, strict : bool ]);

DESCRIPTION

This function is used to check if a key (element) exist in a context

PARAMETERS

context
Specifies the context
 
element
Specifies the key 
 
strict
This parameter has been implemented in version 5.177. Default for strict is true. If the value is false then:
1. exist will return false on undefined value
2. exist will return false if context is not a context

RETURN

A boolean set to tru if element is a key in the context

EXAMPLES

Function `exist` may not give results you might expect. Here is a simple case scenario

user = {"name": "Patrick"};
user.uid;"<br>"; // This line will create a key `uid` inside the `user` context, set as undefined.
user;"<br>"; // You may be confused by the fact that when printing the `user` context, the key `uid` does not appear. Since it does not appear, you may think that the key was indeed not created.
user.exist("uid");"<br>"; // ...but `exist` still reveals its existance.
user.uid == undefined;"<br>"; // Testing directly the key `uid` to undefined, will reveal the true existence of the key.

Output result:


{"name":"Patrick"}
true
true

You may want to test the existance of an attribute but disregard the keys set to `undefined`. To do this, use `strict: false

user.exist("uid", strict: false); 

Output result:

false

Other examples

assoc = { "x" : 1, "y" : 2 }; 
assoc.exist("x"); // return true 
assoc.exist("a"); // return false 
exist(assoc, "y"); //return true;

v = { "x" : 1, "z" : undefined };
exist(v, "x");                                      // return true
exist(v, "y");                                      // return false
exist(v, "z");                                      // return true
exist(v, "x", strict:false);                    // return true
exist(v, "y", strict:false);                    // return false
exist(v, "z", strict:false);                    // return false

v = { "x" : 1 };
v.user = (exist(v, "user"))?"11":"22";
v.user; // return 11

v = { "x" : 1 };
v.user = (exist(strict:false, v, "user"))?"11":"22";
v.user; // return 22

SEE ALSO

AUTHOR

Written by Pierre Laplante and Caroline Laplante, <laplante@sednove.com>

MODIFICATIONS

1.0 2014-09-09 21:24:14 laplante@sednove.com

1.1 2022-04-08 laplante@sednove.com add strict mode

Edit

© 2024 extenso Inc. All rights reserved.