How to create a hash table of json objects in javascript?

I want to create a hash table of json objects where each json object represents a user.

I want to do this in order to create a client client cache.

User { ID: 234, name: 'john', ..); 

Therefore, I can refer to such things:

if (userCache [userid]! = null) alert (userCache [identifier] .id);

Is it possible?

+4
source share
4 answers

Javascript objects themselves are maps, so for example:

 var userCache = {}; userCache['john'] = {ID: 234, name: 'john', ... }; userCache['mary'] = {ID: 567, name: 'mary', ... }; userCache['douglas'] = {ID: 42, name: 'douglas', ... }; // Usage: var id = 'john'; var user = userCache[id]; if (user) alert(user.ID); // alerts "234" 

(I didn't understand if your "userid" would be "john" or 234, so I went with "john" above, but you could use 234 if you want.)

This is before implementing how keys are stored and whether the card is a hash card or some other structure, but I did it with hundreds of objects and was completely satisfied with the performance even in IE, which is one of the slower implementations (at the moment).

This works because there are two ways to get the properties of a Javascript object: through dotted notation and through parentheses with a notation. For instance:

 var foo = {bar: 10}; alert(foo.bar); // alerts "10" alert(foo['bar']); // alerts "10" alert(foo['b' + 'a' + 'r']); // alerts "10" s = "bar"; alert(foo[b]); // alerts "10" 

It may seem strange that this copied syntax for getting an object property by name is the same as getting an array element by index, but in fact the array indices are object properties in Javascript. Property names are always strings (theoretically), but automatic conversion happens when you do things like user[234] . (And implementations are free to optimize the transformation if they can, provided semantics are preserved.)

Edit Some bits and fragments:

Quoting via cache

And if you want to iterate over the cache (and based on your question about the next steps ), you might want others to read this question)

 var key, user; for (key in userCache) { // `key` receives the name of each property in the cache, so "john", // "mary", "douglas"... user = userCache[key]; alert(user.ID); } 

The keys are looped in a certain order, it depends on the browser.

Delete from cache

Suppose you want to completely remove a property from the cache:

 delete userCache['john']; 

Now the object no longer has the "john" property.

+8
source

This is almost verbatim code, but it works, yes:

 var cache = {} cache[234] = { id: 234, name: 'john' } if (cache[1]) alert('Hello ' + cache[1].name); if (cache[234]) alert('Hello ' + cache[234].name); 

Or your question on how to implement this on the server side to get the user cache to the client?

0
source
 function findUser(users, userid) { for (var i = 0; i < users.length; i++) { if (users[i].ID === userid) { return users[i]; } } return null; } var users = [{ ID: 234, name: 'john' }, { ID: 235, name: 'smith' }]; var user = findUser(users, 235); if (user != null) { alert(user.name); } 
0
source

Since all objects in Javascript are truly dictionaries, you can do this in several ways. One of the methods:

 var dict = new Object; dict.Bobby = { ID: 1, Name: "Bobby" }; if (dict.Bobby != null) alert(dict.Bobby.Name); 
0
source

Source: https://habr.com/ru/post/1309574/


All Articles