I encountered this obscure syntax:
const a = {}
const c = [1,2,3]
for (a.b of c) {}
assert(a.b === 3)
How does it work?
It just puts each iteration value into that property value of the existing object.
const a = {}
const c = [1,2,3]
for (a.b of c) {
console.log(a.b);
}
console.log(a.b);
console.log(a);
It can be any existing reference:
const a = {
foo: [
{
nested: 'nestedVal'
}
]
}
const c = [1,2,3]
for (a.foo[0].nested of c) {
console.log(a.foo[0].nested);
}
console.log(a.foo[0].nested);
console.log(a);
Including a standalone variable:
let a;
const c = [1,2,3]
for (a of c) {
console.log(a);
}
console.log('a is:', a);
Or you can create a binding for each iteration with const
or let
, eg const item of c
, as you probably already know.
You can easily have a look at by logging the a
variable.
By assigning a.b
you hold the value of iteration at b
property of a
.
Please have a look at the following snippet:
const a = {}
console.log(a);
const c = [1,2,3]
for (a.b of c) {
console.log(a);
}
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With