Onyooo Onyooo - 1 month ago 20
Javascript Question

Understanding destructuring with for...of

Looking through Atom's source code, I found this use of the

for..of
statement:

for (const {name, duration, x} of results) {
dataByBenchmarkName[name] = dataByBenchmarkName[name] || {points: []}
dataByBenchmarkName[name].points.push({x, y: duration})
}


I'm trying to replicate this iteration in the terminal, but the block never executes (as if I'm missing a bracket). I've tried iterating over objects (shallow, and deep), and arrays. When I take the curly brackets out, the statement works as documented. Can anyone give me a working example with brackets to learn from? Something simple like:

var test = ...;
for(var {x,y,z} of test) {
console.log(x);
};


Thanks

Answer

This works

var test = [{x:1,y:3,z:2},{x:5,y:6,z:3}]
for(var {x,y,z} of test) {
    console.log(x);
}