Skip to content Skip to sidebar Skip to footer

Destructuring Array Into An Object

I am trying to do array destructuring in javascript and encounter some very puzzling behavior Here's the code I have -- let res = { start: {}, end: {},

Solution 1:

Try this:

let res = {
  start: {},
  end: {}
};
[res.start.hour, res.start.minute] = [7, 20]; // Notice the semicolons.
[res.end.hour, res.end.minute] = [17, 30];
console.log(res);

I copy-pasted your code from the first block into VS Code and used 'Alt + Shift + F' (auto-formatting). It fixed the code to the following, indicating that something is clearly off:

let res = {
  start: {},
  end: {}
};
[res.start.hour, res.start.minute] = [7, 20][(res.end.hour, res.end.minute)] = [
  17,
  30
];
console.log(res);

As it turns out, semicolons do matter sometimes in JavaScript.


Post a Comment for "Destructuring Array Into An Object"