6

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?

aaaaaa
  • 169
  • 4
  • 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 Apr 17 '17 at 15:01

1 Answers1

3

Set and Map have slightly different APIs in terms of their stored values. While Sets contain values only, Maps 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.

Andy
  • 10,238
  • 4
  • 25
  • 50
  • 1
    You 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 expose `values` methods that just return the values so it doesn't really answer the question. – Aluan Haddad Dec 18 '16 at 13:25
  • 2
    @AluanHaddad OP was specifically asking why would you ever use the `entries` method, not asking which other methods `Set` and `Map` have. – Andy Dec 18 '16 at 17:38
  • 1
    That is true. However, it is still possible to write a function that works for both sets and maps without the entries method. – Aluan Haddad Dec 18 '16 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` and `Map` anyway. Personally I think the `entries` approach is leaner. – Andy Dec 18 '16 at 19:25
  • 1
    Why not just use the `values` method. I am not suggesting any prototype modifications. – Aluan Haddad Dec 18 '16 at 19:30
  • Ha - I appreciate your guys' discussion as it caused me to google 'mdn entries'. My new understanding isn't that the method was made to be consistent with `Map`, but made to be consistent with all the other containers. This makes way more sense - as I don't feel it appropriate to abstract the `entries` method just for Map and Set alone, but if the alternative is to omit the method for `Set` yet implement it in all other containers, that would easily cause bugs. My conclusion then is mdn's documentation should be updated as it's misleading. – aaaaaa Dec 18 '16 at 20:04
  • @AluanHaddad Once again, the question was what use has the `entries` method, not `values` or `keys`. :) – Andy Dec 18 '16 at 21:47
  • @DavidPacker - I don't like answering my own questions, so if you would reword your answer to generalize beyond just `Set` <--> `Map` consistency I'll mark it as the answer. – aaaaaa Dec 18 '16 at 22:15
  • @DavidPacker rereading this, I believe your points are valid. I would remove my downvote given the opportunity. – Aluan Haddad Mar 11 '17 at 17:01