According to mdn's "Set" documentation, entries
is provided as a convenience method
...to keep the API similar to the Map object, each entry has the same value for its key and value here, so that an array [value, value] is returned.
My question is what is the purpose of keeping the API similar in this case? i.e. when would I ever use the method entries
for a Set?
-
Not enough for a full answer, but one useful corner case is when "attaching" an object instance that is based on previously-retrieved data from a different context (such as having "Get" and "Save" methods in a service layer). In this case, you need to specifically mark the instance you attach as being "Modified" so EF will generate the correct update statement, and you do that through the Entries dictionary.KeithS– KeithS2017年04月17日 15:01:55 +00:00Commented Apr 17, 2017 at 15:01
1 Answer 1
Set
and Map
have slightly different APIs in terms of their stored values. While Set
s contain values only, Map
s contain their associated keys as well.
Because of this, should both of the structures have filter
, map
, forEach
,... functions just like Array
does, the action setting presumtion about each iterated value is likely to be slightly different, specifically:
Map: function(key, value) { }
Set: function(value) { }
Because when you already keep keys in a Map
it makes sense to be able to inspect the value of the key
during iteration as well.
Now if you encountered a situation where you would like to iterate over any of the two collections but cared only about the value, you would need two functions, one for each data collection.
map.forEach((key, value) => console.log(value));
set.forEach(value => console.log(value));
Even though you only care about the value and the logic of both actions is clearly the same, you cannot write a function to serve both collections. The entries
method of both Set
and Map
fixes this issue by providing a common interface.
By creating the following function (or its alternative, I am not manily a JS developer so please excuse any inaccuracies):
function forEach(entriesIterator, action) {
let current = entriesIterator.next();
while (current.done === false) {
action(current.value[1]);
current = entriesIterator.next();
}
}
you can feed either entries
of Map
and/or Set
and it will still work.
forEach(map.entries(), value => console.log(value));
forEach(set.entries(), value => console.log(value));
TLDR: The entries
method provides an abstraction to iterate over a data collection where you may potentially only care about the values.
-
1You have the right idea conceptually but nothing prevents the function from taking the value first and the key second, which is precisely what a callback passed to
Map.prototype.forEach
must do because it receives (value, key) for each pair. Also, Map and Set also exposevalues
methods that just return the values so it doesn't really answer the question.Aluan Haddad– Aluan Haddad2016年12月18日 13:25:46 +00:00Commented Dec 18, 2016 at 13:25 -
2@AluanHaddad OP was specifically asking why would you ever use the
entries
method, not asking which other methodsSet
andMap
have.Andy– Andy2016年12月18日 17:38:48 +00:00Commented Dec 18, 2016 at 17:38 -
1That is true. However, it is still possible to write a function that works for both sets and maps without the entries method.Aluan Haddad– Aluan Haddad2016年12月18日 17:57:17 +00:00Commented Dec 18, 2016 at 17:57
-
1@AluanHaddad Even from the little experience with JS I have I really do not like altering prototypes of system types. Seems like monkey patching and you would need to alter prototypes of both
Set
andMap
anyway. Personally I think theentries
approach is leaner.Andy– Andy2016年12月18日 19:25:45 +00:00Commented Dec 18, 2016 at 19:25 -
1Why not just use the
values
method. I am not suggesting any prototype modifications.Aluan Haddad– Aluan Haddad2016年12月18日 19:30:44 +00:00Commented Dec 18, 2016 at 19:30