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.
Related
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 🙃🙃
This question already has answers here:
Are Javascript arrays primitives? Strings? Objects?
(7 answers)
Closed 3 years ago.
I have had discussions with seasoned programmers who do not think arrays are objects; they think that objects and arrays are of separate types of the data structure category. But, as far as I understand, objects and arrays are of the same type.
If you were to use the unary typeof operator to evaluate a binding storing an array, the JavaScript interpreter will return the string "object." Moreover, you can use the Object.keys() method on a binding containing an array to get the property names of the array object.
When studying programming, I see authors use the phrase "objects and arrays," as if these were separate entities, which adds to the confusion. I go to W3Schools, and in the section "Data Types," they list arrays and objects as if they are not of the same data type.
As far as I understand it, objects and arrays are the same, except array objects are structured different than objects delimited by braces. Perhaps I'm missing something and can be enlightened.
Are arrays objects? If so, shouldn't they be listed as the same type, and shouldn't the phrase be "arrays and other objects"? If not, why not?
(Edit: I know the question of if objects and arrays are the same has been asked, but I was also asking if there should be a change in how arrays and objects are discussed, so people don't think that arrays are not objects, since programming is not the easiest subject to digest.)
Arrays are indeed a type of object.
console.log([] instanceof Object);
I go to W3Schools, and in the section "Data Types," they list arrays and objects as if they are not of the same data type
W3Schools is not a particularly trustworthy source. That said, when discussing organization of data, it's pretty common to talk about arrays (an ordered collection of values) as distinct from objects (a usually-unordered collection of key-value pairs), despite the fact that one is a subtype of the other, because the fact that arrays inherit from Object.prototype often isn't something one needs to consider when organizing / sorting through data.
If one tries to use any of the Object methods on an array, it's certainly essential to remember that Array inherits from Object, but usually that's not necessary - often, one will be only be using array methods instead (like forEach, find, includes, etc).
This question already has answers here:
Why can I add named properties to an array as if it were an object?
(8 answers)
Difference between array and object in javascript? or Array Vs Object
(4 answers)
Associative array versus object in JavaScript
(7 answers)
Closed 4 years ago.
From my readings, in JavaScript:
Objects = Hash Tables, which are build on Arrays. However, it is commonly said that Arrays are Objects in JS. How are these two concepts reconciled?
Objects are not built on arrays. Objects have their own optimizations.
In general:
Objects are for "structs", structures of predictable "shape" and keys known in advance (even though they can be used with dynamic keys, you should use Maps for that. See below).
Arrays are for lists (and queues, and stacks), structures where the keys are numbers, or where the order of elements matters. Arrays are "special" objects, not the other way around. (You can put string-based properties on an array, just like any object. Please don't do that though).
Maps are for hash tables/dictionaries, structures where the keys are dynamic and not known in advance.
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?
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.