When attempting array destructuring in JavaScript, I encountered some perplexing behavior.
Below is the code snippet causing the issue:
let result = {
start: {},
end: {},
};
[result.start.hour, result.start.minute] = [7, 20]
[result.end.hour, result.end.minute] = [17, 30]
console.log(result)
The resulting output is:
{ start: { hour: 17, minute: 30 }, end: {} }
Strangely, [17, 30] were assigned to res.start instead of res.end.
Interestingly, if I insert a console.log statement like so:
let result = {
start: {},
end: {},
};
[result.start.hour, result.start.minute] = [7, 20]
console.log(JSON.stringify(result));
[result.end.hour, result.end.minute] = [17, 30]
console.log(result)
Then it works as expected. Now, results.end receives the correct values.
I have searched and reviewed the MDN documentation on destructuring but have not found an explanation for this behavior. Any help would be greatly appreciated. Thank you in advance.