1

I'm using a hash table and I'm trying to check for object existence. However I haven't been successful in figuring out how to do this. Could someone help guide me with this. Thanks.

current code.

When clientId equals field id and has item id return true, else add to saved_tokens.

var saved_tokens = {}; 

if ($.inArray(item.id, saved_tokens) == -1) { 
    saved_tokens.push[clientId] = item.id;
}
3
  • Note that you can never call functions without parens. Currently you're setting a property on the .push function. Commented Mar 5, 2012 at 20:28
  • Are you trying to check if the key clientId already exists in the object, or if the value item.id already exists? Commented Mar 5, 2012 at 20:28
  • I have multiple clientId's that could share the same items. I need to test the clientId to see if it contains an Item, if not add it to the the saved_toekens with the clientId. Commented Mar 5, 2012 at 20:31

2 Answers 2

5

Don't use jQuery for that. Use pure JavaScript:

if (!saved_tokens.hasOwnProperty(clientId)) { // If clientId is not in the hash
    saved_tokens[clientId] = item.id;
}

.push is an array method. A {} creates an object. Since this object is not an array, it doesn't have any array methods.

Sign up to request clarification or add additional context in comments.

2 Comments

Does this also prevent duplicate item.id's from being added to the hash?
@George No. You have two options: 1) Also maintain a hashtable for the item.id, or 2) Loop through all saved_tokens, and check whether the value is (not) equal to item.id.
0

I personally use 'typeof'.

var saved_tokens = {}; 

if (typeof(saved_tokens[clientId]) == 'undefined') { 
    saved_tokens[clientId] = item.id;
} 

7 Comments

Noting that this doesn't distinguish between whether clientId exists as a key in the object with the value undefined or if clientId doesn't exist as a key at all. if (clientId in saved_tokens) tests if it has been defined as a key (with any value).
@nnnnnn I dont get your comment. Do you mean to say that if clietId having value 'undefined' and clientId doesnt exits both will be true with typeof ?
Yes. If saved_tokens has a key clientId but the value associated with that key is undefined then typeof will return "undefined" just as it also would if saved_tokens does not have a key clientId at all. If you need to distinguish between those two possibilities then use if (clientId in saved_tokens), or if you need to know if the property is directly set on the object (and not inherited through the prototype chain) use saved_tokens.hasOwnProperty(clientId)
@nnnnnn May be I am getting your answer wrong but this works, jsbin.com/ayocin/2/edit
Sorry, your link didn't work for me (might be an IE problem). I'm not saying typeof won't work (it does!), I'm saying it doesn't distinguish between the two cases that might cause the expression saved_tokens[clientId] to be undefined - which for some purposes doesn't matter.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.