Javascript, Map VS Object what are the Object default keys - javascript

While I totally understand that keys in Map can be any type, but keys in object are converted automatically to strings,
however when I refere to MDN article about Maps,
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
I couldn't understand this part
I beleive that Map has a prototype by default already per the below screenshot I take while I was exploring
Another shot after filling some data
So do you know what did they mean with Object has a protptype,so it contains default keys
since Map already has a prototype as well, it's supposed to have those keys as well
I'm wondering šŸ™ƒšŸ™ƒ

Related

Hashtable vs objects In javascript

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.

ES2015 - Sets, Maps and Array Questions

I'm just looking into ES2015, and coming up to Maps, Sets and Arrays.
Question 1
Firstly I'm interested in why they all use a different method to add items to it.
Set.add("item");
Map.set("item");
Array.push("item");
is there method to the madness rather than keeping them all as .push?
Question 2
size vs length.
Why have Map and Set got .size but Array has .length why not use the same?
Question 3
When would you use Map over Array? can anybody give a real world example for this, I understand you can do things like use objects as keys in Maps, but why would you do that in the first place.
Hopefully somebody can clear this up to help inform other new starters to ES2015, thanks.
Sets, Maps and Arrays use different methods, because these methods do different things. Array.prototype.push() adds one or more elements to the end of the array. Set.prototype.add() works similarly, but it only accepts one argument. If it was named push(), some people would think that it works the same as the array method, and they'd try doing set.push(1, 2, 3) and they'd be confused why it adds only the first element.
Map.prototype.set() is a completely different thing. From MDN:
The set() method adds or updates an element with a specified key and value to a Map object.
If an element with a specified key already exists, this method doesn't add any element to the Map, but only updates the value of that element.
You second question was answered on this blog:
length is for sequences, data structures that are indexable ā€“ like arrays. size is for collections that are primarily unordered ā€“ like maps and sets.
I don't really understand your third question. Maps and Arrays are completely different data structures. Maps are similar to objects, see Maps vs Objects in ES6, When to use?

What is the difference between a dictionary and a Map in Javascript 6? [duplicate]

This question already has answers here:
Map vs Object in JavaScript
(15 answers)
Closed 7 years ago.
How is a Map different from a dictionary/object?
In other words, what is the difference between let x = {} and let x = new Map() ?
Objects and maps compared (from MDN):
Objects are similar to Maps in that both let you set keys to values,
retrieve those values, delete keys, and detect whether something is
stored at a key. Because of this (and because there were no built-in
alternatives), Objects have been used as Maps historically; however,
there are important differences between Objects and Maps that make
using a Map better:
An Object has a prototype, so there are default keys in the map.
This could be bypassed by using map = Object.create(null) since ES5,
but was seldomly done.
The keys of an Object are Strings and Symbols, where they can be
any value for a Map.
You can get the size of a Map easily while you have to manually
keep track of size for an Object.
This does not mean you should use Maps everywhere, objects still are
used in most cases. Map instances are only useful for collections, and
you should consider adapting your code where you have previously used
objects for such. Objects shall be used as records, with fields and
methods. If you're still not sure which one to use, ask yourself the
following questions:
Are keys usually unknown until run time, do you need to look them up dynamically?
Do all values have the same type, and can be used interchangeably?
Do you need keys that aren't strings?
Are key-value pairs often added or removed?
Do you have an arbitrary (easily changing) amount of key-value pairs?
Is the collection iterated?
Those all are signs that you want a Map for a collection. If in
contrast you have a fixed amount of keys, operate on them
individually, and distinguish between their usage, then you want an
object.

When would you use an ES6 Map over an Object? [duplicate]

This question already has answers here:
Map vs Object in JavaScript
(15 answers)
Closed 7 years ago.
I've been looking at the new ES6 docs on MDN and I can't find a real world use for the Map object where a normal object wouldn't work. Does anyone have any use cases for Maps and explain why an object wouldn't work in that scenario?
MDN lists a number of important differences:
An Object has a prototype, so there are default keys in the map. [editor: the good old hasOwnProperty issue]
The keys of an Object are Strings, where they can be any value for a Map.
You can get the size of a Map easily while you have to manually keep track of size for an Object.
A Map iterates its elements in insertion order, whereas iteration order is not specified for Objects.
So a Map is finally an insert-ordered key-value store for Javascript, which additionally allows mapping any value to any value, instead of restricting keys to be strings. This can greatly simplify some code where ordering is important, or where objects or other complex data types need to be associated with other data.

Map/Object in javascript does not support Element as key

My question is how to use Element as a key in vanilla javascript. Before ES6, Object and other map-liked structures use toString() to identify the keys -- which means it is impossible to distinguish Elements.
https://code.google.com/p/closure-library/issues/detail?id=541 is for google closure library, but vanilla javascript has exactly the same issue.
ES6 Map resolves this, which is great! But as usual, how about old browsers that does not support ES6...
The usual solution to this problem in ES5 is to generate a unique string key (A monotomically increasing number turned into a string works just fine), assign it to the DOM object as a custom property and to then use that string as the key in the Object/Map.
This gives you a unique string, you can use as the map key and if you are given the DOM object again and asked to look it up in the map, you can just check it for the special key and, if found, use it to check the map. If the DOM object does not have the unique key, then it must not have been in one of these maps yet.
Here's a Javascript object that implements this automatically (objectSet): https://github.com/jfriend00/Javascript-Set/blob/master/objectset.js which is related to the code in this answer: Mimicking sets in JavaScript?.
Or, here's a partial polyfill for the ES6 Set object that implements this string key generation automatically when using objects in the Set.

Categories

Resources