1

I'm finding it hard to understand how the following code snippet (from here) works.

const { result: results } = await client.database(databaseId).container(containerId).items.query(querySpec, {enableCrossPartitionQuery:true}).toArray();
for (var queryResult of results) {
     let resultString = JSON.stringify(queryResult);
     console.log(`\tQuery returned ${resultString}\n`);
}

Why is the iteration done on results rather than result because as I understand we are creating an object with a property named result and there is no variable elsewhere declared with the name results?

snippetkid
  • 282
  • 4
  • 16
  • `{ result: results }` puts the `result` property into a variable named `results` – CertainPerformance Jul 15 '19 at 09:32
  • It's called destructuring assignment. It gets the `results` property from the right hand object, instantiates a new const named `result` and assigns that value there. Check here more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment – Stefan Hariton Jul 15 '19 at 09:36
  • 2
    `const { result: results } = someObject;` is equivalent to `const results = someObject.result;` – Thomas Jul 15 '19 at 09:38
  • @Thomas - Thanks for the simple explanation! I get it now. I thought it was always `{property:value}`. This was new to me! – snippetkid Jul 15 '19 at 09:54

0 Answers0