Hello I don't undersand the following destructuring assignment: const seasonConfig = { summer: { text: "Lets hit the beach", iconName: "sun", }, winter: { text: "Burr, it is chilly", iconName: "snowflake", }, }; const getSeason = (lat, month) => { if (month > 2 && month < 9) { return lat > 0 ? "summer" : "winter"; } else { return lat < 0 ? "winter" : "summer"; } }; const season = getSeason(37.23452, new Date().getMonth()); const { text, iconName } = seasonConfig[season]; Code (JavaScript): seasonConfig are nested objects, right. However, when I fetch the season (summer or winter), I put it into an array-like structure: seasonConfig[season]; for the destructuring assignment Why is this? Season config is obviously not an array!? Thanks a lot!!
yes, it's because seasonConfig[season] is a associative array e.g. seasonConfig["summer"] and with that, it is an object
Technically this isn't destructuring, it's object assignment. When an object assignment has no name, the names inside it are used to index the target instead. This can be REALLY confusing given that if text and iconName were local variables, they'd be used instead of the values from the assignment. It's one of the reasons I hate this type of construct as depending on where you use it and what variables are and are not assigned, Christmas only knows how JS is going to handle said code. One of the many reasons JS is an ugly kludge, and I don't entirely grasp why people are in such a rush to use said shit-show server-side. We only ever used it client-side because it was all we had.