I’m new to data structure and I’m learning it in Javascript.
My Question is:
Why do we need hash tables when we 've objects in javascript?
Can anybody give me a situation where hash tables will be more useful than objects?
"Hashtable" is called different things in different languages. Java has Hashtable and HashMap, Ruby has Hash, Python has dict... in JavaScript, it's called Map.
Objects' keys are limited to strings; Map keys can be anything.
Objects support inheritance; a Map only contains what is specifically put into it.
Think you means Map instead of HashTable. IMHO Map may be more useful and perform better if you need one of that:
keep order of insertions of key/value pairs;
frequent additional and removal;
key which not String/Symbol.
I think you can obtain more information at MDN
The MDN docs on this are quite helpful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Objects_and_maps_compared
Most notably, using a map gives you the advantage of using anything as a key, maps retain order, and may perform better when constantly adding and removing values.
Related
If I have a JSON file that let's say represents a dictionary of words, where the word is the key and the value is the definition of that word. Would sorting and organizing the keys in alphabetical order in the JSON file make a difference to the performance when searching a word to find its definition (plus maybe other details about that word) in JS - that is if there were thousands of words or even more?
Or does JSON and Javascript already have an algorithm built in to find results optimally without the need to sort the data for better performance?
Also, I wouldn't mind having an alternative data structure or format or library that could give faster results for this kind of search problem suggested to me! (but of course, this suggestion isn't part of the main question)
There is no need to sort the keys in and json structure. The reading of a key/value will not be faster after sorting. See the discussion here
If you have very big structures, you can implement a TRIE by yourself. See: Wiki TRIE. Or see this: trie.js
Sorting the keys does not impact the search performance. Javascript objects may not remember the order of insertion
If possible, use Map object - The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
I am trying to write documentation on a piece of Javascript code but I am having trouble describing the objects made by the code in a concise and understandable way. It is especially difficult because the objects have nested objects (often multiple layers).
Is there any mathematics that involves things with keys and attached values?
If not, how best can I describe an object with multiple nest objects in a concise manner?
Note: Just showing an example of an object is not enough as the structure changes often. Also, there are mathematical relationships between the keys and the values (coupon dates as keys and coupon payments as values).
I would say that Javascript objects are functions or mappings, in that they map keys to values.
Beyond that, it is hard to compare... the domain can encompass numbers, and a subset of all strings. As simple as that is to say, I'm not sure what mathematical field (etc) the domain would be equivalent to!
The range would, of course, be worse, as values in the range can be numbers, strings, booleans, undefined, further objects, or functions. However, I think the concept of an object being a mapping is fairly intuitive.
This doesn't include the prototype style inheritance, but I'm not sure how deep you want to go...
I saw a comment on it earlier, JavaScript objects pretty much follow the associative array abstract data type, which is a mathematical concept by virtue since computer science is basically a subset of applied mathematics, but if you need a true mathematical representation there's relational algebra which was created for relational databases (close enough) and is essentially an extension of set theory... just remember math doesn't necessarily mean it's clear and concise – Patrick Barr yesterday
It seems that everything you can do with Set you can do with Map? Is this correct?
What are the semantic differences between a Set and a Map?
Edit: the linked "dupe" does not enumerate the semantic differences between the two.
I have retracted my close vote.
A quick google of 'set vs. hashtable' or 'set vs. hashmap' turns up numerous SO questions, mostly in the Java tag, but I didn't see a single answer that actually tackled the difference in a good conceptual way (although a few linked to relevant resources).
Let's start with what data structures are: namely containers for values. The values can be whatever for the most part. Some data structures are homogeneous, some aren't. Some have restrictions (e.g. Map can have arbitrary keys but POJOs can only have string or symbol keys), some don't, some are ordered, some aren't, etc. All of these tradeoffs generally boil down to performance.
A Set is a data structure that holds unique values. Let's compare to an array:
Array.from(new Set([1,2,2,3])).toString() === [1,2,2,3].toString();
// false
Like arrays or lists, Sets in JavaScript are linear: you can traverse them in order*. But unlike arrays (more like lists) Sets are not indexed. You can't say new Set(1)[0];.
Maps on the other hand *ahem* map keys to values (indexed). If I have a Map new Map([['a',1]]), then .get('a') will return 1. Order is not generally considered important for Maps, that what key indexes are for. Nor is uniqueness: new Map([['a', 1], ['b', 1]]) stores the value 1 twice** and you can access it from either key.
Even if, like me, you are primarily a self-taught programmer, I highly recommend familiarizing yourself with basic data structures as it offers valuable insight into problem identification and general solutions. If you find yourself using Array.prototype.shift a lot for instance, you probably wanted a FIFO queue/linked list instead.
* Sets in general are unordered, the retention of insertion order is a JavaScript thing.
** The underlying implementation may as an optimization store it only once, but that is an implementation detail and opaque to you the user.
i wanted to use Google Apps Scripts to analyze a Google Sheet.
I want to analyze how common certain answers are and wanted to use a map or dictionary to implement this.
My problem is, no suitable datatype seems to work inside Google Apps Script. I also read the tipp to just use objects with a key value, but it doesnt seem like Google Apps Scripts support the values parameter, generally associated with javascript objects.
So are there any types of key-value datastructure, that i can use and that has working functions for giving out the keys or values ?
Are you looking for a datastructure to store and retrieve map type data or is it just for processing?
The PropertiesServices and the CacheServices are the closest to what you are looking for. However, they take only strings as keys and values so you can build a mechanism on top of it to support objects in addition to string values.
You can use this helpful library that was created by github user yinonavraham that supports putting/getting objects in Cache
https://github.com/yinonavraham/GoogleAppsScripts/tree/master/EnhancedCacheService
While yinonavraham has built it for the CacheService, you can easily build an equivalent for the PropertiesService following his lead.
I am in need of a Javascript data structure which will allow me to insert strings and search for strings efficiently. I have been looking around and the only data structures I have come across are objects and arrays. Objects are more used for encapsulation and cannot really be used for searching and using arrays can be slow. Are there any other data structures that will allow me to insert and search strings efficiently? Right now at best I could do a binary search on an array. Any other ideas? Thanks.
Objects are more used for encapsulation and cannot really be used for
searching
That was true in classical languages, not so true in JS.
var obj = { memberone: "value1" }
var value = obj["memberone"];
//value === "value1"
Objects can be searched in JS. Bear with me...
and using arrays can be slow.
Yes, can be - but don't have to be.
Are there any other data structures that will allow me to insert and
search strings efficiently?
Data structures? No. Again that is a classical perspective. In JS, it is different.
Check out _underscore.js.
It is 4k min gzip.
It provides a number of advanced iterator helpers (so you don't have to)
It provides templates to display your data to screen efficently.
It will benefit the rest of your development, maintenance, and implementations.
This is a good example of JS flexibility.
Hope that helps.
All the best!
Nash
There are some more I found after some goggling,
Javascript data structures - a collection object
One more thing you can use json objects and its JavaScript API to manipulate same. Please refer same here