So I know you can do object destructing like: const { item } = data;
Also array destructing like: const [ item ] = data;
You can also do this in function params like: const x = ({ item }) => item;
And I've seen lots of questions and answers on it. However I haven't seen an example and a good explanation of nested objects in an array.
const test = [{ count: 1 }];
const [{ count }] = test;
I'd normally do:
const x = test[0];
const { count } = x;
It was only today while testing in codepen that I figured out you could destructor them both within the same assignment.
Could anyone explain what's going on when I'm doing [{ count }]? Because I'm doing array destructing with the const [] = test but I'm not destructing anything so that obviously fails. If I then { count } within that I get the value out I want.
I can't break it down enough to understand how it's working. I'd assume [] = test is the same as test[0] then i do { count } = test[0]. But I'd just like to understand how it's working more.
I did have a look through some of the MDN docs and stuff but I can't find a good explanation on the above scenario I mentioned.
Thanks!
const [ , , { count }] = [{ apple: 1}, {pear: 5}, {count: 13}, {delta: 2}]assigns 13 tocount?const [temp] = test, {count} = temp;const [aVarName] = test;and that will be the equivalent ofconst aVarName = test[0];. I thought You're always unpacking values and you can assign defaults but any value you pass into the[]it sees as a param to destructor