Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #35233

Re: using Map

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: using Map
Date 2018-06-14 17:13 +0200
Organization PointedEars Software (PES)
Message-ID <5317938.iMaMEg0OTm@PointedEars.de> (permalink)
References (2 earlier) <AuqdnSaYXed-CL3GnZ2dnUU7-LnNnZ2d@westnet.com.au> <18902741.ZdnXStS6ea@PointedEars.de> <s5adnVTPJv1_y7_GnZ2dnUU7-SnNnZ2d@westnet.com.au> <1854071.sLym9PP3UP@PointedEars.de> <RbydnWCQP62s7r_GnZ2dnUU7-bnNnZ2d@westnet.com.au>

Show all headers | View raw


Andrew Poulos wrote:

> After experimenting with creating and then getting and setting property
> values of an object that is a Map's value I went on to work with
> multiple Maps each of which is meant to start with the same values.

OK, but your follow-up had nothing to do with what what you posted before 
and what I suggested to do instead.

We cannot have a useful discussion if you post every stray thought that just 
entered your mind, without regard to what was said before.
 
>>> when I changed the value in one map the corresponding value in all maps
>>> changed as well.
>> 
>> No.  Why do you not read the Specification?  Why do you not try it out?
> 
> I populated a map using an array of objects.
> I created multiple copies of the map.

How?

> When I changed the property value in one of a map's value all the
> corresponding values in the other maps changed as well.

Then you did something wrong.  Here is how you efficiently construct, 
populate, and clone a Map instance:

| > var map = new Map([["foo", "bar"]]);
| <- undefined
| 
| > map
| <- :> Map(1) {"foo" => "bar"}
| 
| > var map2 = new Map(map);
| <- undefined
| 
| > map2
| <- Map(1) {"foo" => "bar"}
| 
| > map2.set("foo", "baz");
| <- Map(1) {"foo" => "baz"}
| 
| > map2
| <- Map(1) {"foo" => "baz"}
| 
| > map
| <- Map(1) {"foo" => "bar"}
 
> I have looked at the spec but I didn't see anywhere that dealt with
> making copies of a Map.

,-<http://www.ecma-international.org/ecma-262/8.0/#sec-map-iterable>
| 
| 23.1.1.1 Map ( [ /iterable/ ] )
| 
| When the Map function is called with optional argument, the following
| steps are taken:
| 
| 1. If NewTarget is *undefined*, throw a *TypeError* exception.
| 2. Let /map/ be ? OrdinaryCreateFromConstructor(NewTarget,
|    "%MapPrototype%", « [[MapData]] »).

This creates a new empty Map instance referred to by /map/.

| 3. Set /map/.[[MapData]] to a new empty List.
| 4. If /iterable/ is not present, let /iterable/ be *undefined*.
| 5. If /iterable/ is either *undefined* or *null*, let /iter/ be undefined.
| 6. Else,
|    a. Let /adder/ be ? Get(/map/, "set").

(For brevity, in the following I am using the wording “Array” instead of
the precise wording “reference to an Array instance”.)

This is where Map.prototype.set() is defined as the method to use to add 
items to the new Map, so that passing an Array (/iterable/) of Arrays
to the constructor is functionally equivalent to consecutive calls of 
Map.prototype.set() immediately after construction.  (BTW, as .set()
returns the Map, you can also chain .set() calls – another way to DRY,
and a way to have a populated Map without a name to store the reference
to it).

|    b. If IsCallable(/adder/) is *false*, throw a *TypeError* exception.
|    c. Let /iter/ be ? GetIterator(/iterable/).

This is where the iterator for a passed Array is retrieved.  It follows
that if /iterable/ is a Map instead, *its* iterator is retrieved, which 
returns key-value pairs as a 2-element Array – so for cloning a Map, you
can simply pass a Map to the Map constructor (also see “NOTE” below).

    [This is a useful feature that one should provide for one’s custom
     object types.  Calling a “clone” or conversion method should be
     the exception.]

Currently, the MDN article mentions this only implicitly.  It is saying:

  |   iterable
  |     An Array or other iterable object whose elements are key-value pairs
  |     […]
  | 
  | Description
  | 
  |   A Map object iterates its elements in insertion order — a for...of
  |   loop returns an array of [key, value] for each iteration. […]

    [I did not know for sure as I had not read the Specification for this,
     so *I* *just* *tried* *it* *out*.]

| 7. If /iter/ is *undefined*, return map.

If the passed object is not iterable, the constructed Map is empty.

| 8. Repeat,

This is where iteration will commence.

|    a. Let /next/ be ? IteratorStep(iter).
|    b. If /next/ is *false*, return /map/.

This is the case if all elements of the Array or items of the Map have been 
processed, so iteration will stop, and the new Map will be returned.

|    c. Let /nextItem/ be ? IteratorValue(next).
|    d. If Type(/nextItem/) is not Object, then
|       i.  Let /error/ be Completion{[[Type]]: throw,
|           [[Value]]: a newly created *TypeError* object,
|           [[Target]]: empty}.
|       ii. Return ? IteratorClose(/iter/, /error/).
|    e. Let /k/ be Get(/nextItem/, "0").
|    f. If /k/ is an abrupt completion, return ? IteratorClose(/iter/, /k/).
|    g. Let /v/ be Get(nextItem, "1").
|    h. If /v/ is an abrupt completion, return ? IteratorClose(/iter/, /v/).
|    i. Let status be Call(/adder/, map, « k.[[Value]], v.[[Value]] »).
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is where each element/item will be added as a key-value pair.

|    j. If status is an abrupt completion, return ? IteratorClose(iter, 
|       status).
| 
| NOTE
| 
| If the parameter iterable is present, it is expected to be an object that
| implements an @@iterator method that returns an iterator object that
                                               ^^^^^^^^^^^^^^^^^^^^^^^
| produces a two element array-like object whose first element is a value
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| that will be used as a Map key and whose second element is the value to
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| associate with that key.
  ^^^^^^^^^^^^^^^^^^^^^^^^

Which I said above.

> (BTW I find the spec intimidating.)

You must learn to control your emotions so that they do not control you.

In fact, although the ECMAScript Language Specification necessarily has 
become more complex as language features were added in each Edition, its 
usability has steadily improved.  Now there is

  ∙ a scroll-sensitive table of contents (TOC) as
  ∙ a tree-view in
  ∙ a responsive sidebar,
  ∙ an input control for full-text search in the TOC above that, and

  in the text,

  ∙ all referenced algorithms can be navigated to, and
  ∙ all variable references can be highlighted,

  by hyperlinks.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

get set value in array of objects Andrew Poulos <ap_prog@hotmail.com> - 2018-06-12 10:50 +1000
  Re: get set value in array of objects "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-06-12 16:40 +0200
  Re: get set value in array of objects "Christoph M. Becker" <cmbecker69@arcor.de> - 2018-06-12 17:12 +0200
  Re: get set value in array of objects Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2018-06-12 17:36 +0200
    using Map Was: Re: get set value in array of objects Andrew Poulos <ap_prog@hotmail.com> - 2018-06-13 13:56 +1000
      Re: using Map Was: Re: get set value in array of objects Andrew Poulos <ap_prog@hotmail.com> - 2018-06-13 14:43 +1000
        Re: using Map Was: Re: get set value in array of objects Andrew Poulos <ap_prog@hotmail.com> - 2018-06-13 17:17 +1000
          Re: using Map Was: Re: get set value in array of objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-13 23:32 +0200
      Re: using Map Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-13 18:47 +0200
        Re: using Map Andrew Poulos <ap_prog@hotmail.com> - 2018-06-14 21:52 +1000
          Re: using Map Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-14 14:31 +0200
            Re: using Map Andrew Poulos <ap_prog@hotmail.com> - 2018-06-14 23:52 +1000
              Re: using Map Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-14 17:13 +0200
                Re: using Map "Mark-" <nospam@mapson.xyz> - 2018-06-14 16:25 +0000
          Re: using Map "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2018-06-14 16:11 +0200
  Re: get set value in array of objects Ben Bacarisse <ben.usenet@bsb.me.uk> - 2018-06-12 16:40 +0100
    Re: get set value in array of objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-13 05:18 +0200
      Re: get set value in array of objects Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2018-06-13 05:25 +0200
  Re: get set value in array of objects Julio Di Egidio <julio@diegidio.name> - 2018-06-14 07:11 -0700

csiph-web