K
K
Konstantin2020-01-31 13:55:41
typescript
Konstantin, 2020-01-31 13:55:41

What is this code doing in the loop?

for (const [node, linkedLayers] of groups) {}

groups is Map()

Do I understand correctly that the key flies to the node variable, and the values ​​to linkedLayers . But why in square brackets instead of {}?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Shvets, 2020-01-31
@Junart1

https://developer.mozilla.org/en/docs/Web/JavaScri...
read the section "Map iteration with for..of"
Why so? Well, because the built-in Map iterator returns an array of [key, value], those are the rules of the language. We can say that this is a shorthand for map.entries().
It also works in the opposite direction, such as

const first = new Map([
  [1, 'one'],
  [2, 'two'],
  [3, 'three'],
]);

A
abberati, 2020-01-31
@abberati

Array destructuring

A
Alexander, 2020-01-31
@NeiroNx

This is a group assignment

c = [1,2];
[a,b] = c;

a=1 and b=2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question