+ 1

I don't understand this for-of loop. How I get this result. Is this some kind of destructuring in for-of?

arr=[] for(let {x=3,y=2} of [{x:1},{y:4}]){ arr.push(x,y) } arr.forEach(k=>console.log(k)) // 1 2 3 4

4th Jan 2023, 9:50 PM
Đorđe Labat
Đorđe Labat - avatar
2 ответов
+ 1
Yes you are correct, it is destructuring combined with defaulting. The code loops through a list of two objects, and extracts two variables x and y. But the first object contains only x, so y is defaulted to 2. The second object contains only y, so x is defaulted to 3. And in each iteration the two values are added to the array. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
5th Jan 2023, 4:47 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thanks for simple answer :)
5th Jan 2023, 5:41 AM
Đorđe Labat
Đorđe Labat - avatar