JavaScript How to new WeakMap with array as parameter?

I have been reading MDN docs about WeakMap. And it mentions the syntax:

new WeakMap([iterable])

But when I tried this, error occurred:

var arr = [{a:1}];
var wm1 = new WeakMap(arr);

Uncaught TypeError: Invalid value used as weak map key

Could you please offer me an example about how to do it via an array?

Answer:1

The documentation says:

Iterable is an Array or other iterable object whose elements are key-value pairs (2-element Arrays).

{a: 1} is an object, not a 2-element array.

Further down it says:

Keys of WeakMaps are of the type Object only.

So you can't use a string as a key in a WeakMap.

Try:

var obj = {a:1};
var arr = [[obj, 1]];
var wm1 = new WeakMap(arr);
console.log(wm1.has(obj));
Answer:2

You need a 2D array, like [[key1, value1], [key2, value2]]. As you don't have keys a WeakSet would be more appropriate here.

Answer:3

Something about the following combination of "multiple" and "as" in the options expression is making ng-options set pristine to false when the form first loads. Is this an angular bug? Does anyone ...

Something about the following combination of "multiple" and "as" in the options expression is making ng-options set pristine to false when the form first loads. Is this an angular bug? Does anyone ...

So I'm fresh into AngularJS, trying to build my first application. And I'm stuck at routing. The first line works, which is just to load a view when the site is entered, but the next one is just ...

So I'm fresh into AngularJS, trying to build my first application. And I'm stuck at routing. The first line works, which is just to load a view when the site is entered, but the next one is just ...

  1. angularjs routing doesn't work

From MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this, it says: In strict mode, however, the value of this remains at whatever it was set to when entering the ...

From MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this, it says: In strict mode, however, the value of this remains at whatever it was set to when entering the ...

I have two controllers, and a directive. Both controllers have the same type of data, an array of filters. Both controllers use the same functions to manipulate this data, but the functions are ...

I have two controllers, and a directive. Both controllers have the same type of data, an array of filters. Both controllers use the same functions to manipulate this data, but the functions are ...