如何在索引不为0的Javascript数组上使用数组分解?
我有以下变量:如何在索引不为0的Javascript数组上使用数组分解?,javascript,arrays,destructuring,Javascript,Arrays,Destructuring,我有以下变量: $firstSection = $main.children[0]; $secondSection = $main.children[1]; 要对第一个变量使用数组分解,我可以这样做:[$firstSection]=$main.children 但是,如何对第二个变量使用数组分解?谢谢。只需将要分解的第二项放在第一项右侧,以逗号分隔的列表中即可。它看起来非常类似于声明包含2项的数组 const [$firstSection, $secondSection] = $main.ch
$firstSection = $main.children[0];
$secondSection = $main.children[1];
要对第一个变量使用数组分解,我可以这样做:[$firstSection]=$main.children代码>
但是,如何对第二个变量使用数组分解?谢谢。只需将要分解的第二项放在第一项右侧,以逗号分隔的列表中即可。它看起来非常类似于声明包含2项的数组
const [$firstSection, $secondSection] = $main.children;
只需将要分解的第二项放在第一项的右侧,以逗号分隔的列表中。它看起来非常类似于声明包含2项的数组
const [$firstSection, $secondSection] = $main.children;
这些值通过逗号分隔的列表访问,因此:
const [$firstSection, $secondSection] = $main.children;
console.log($secondSection); // The second value in the $main.children array
如果您实际上不需要数组中的第一个值,不管出于什么原因,您实际上可以使用逗号省略第一个值
const [, $secondSection] = $main.children;
console.log($secondSection); // The second value in the $main.children array
这些值通过逗号分隔的列表访问,因此:
const [$firstSection, $secondSection] = $main.children;
console.log($secondSection); // The second value in the $main.children array
如果您实际上不需要数组中的第一个值,不管出于什么原因,您实际上可以使用逗号省略第一个值
const [, $secondSection] = $main.children;
console.log($secondSection); // The second value in the $main.children array
这回答了你的问题吗?在JavaScript变量中使用$
并不是真正必要的,也不是建议的。我之所以在这里使用它,是因为它是我目前正在使用的JS样式指南的一部分。你为什么认为不建议这样做@Tadmannat这是一个非常奇怪的风格指南。这能回答你的问题吗?在JavaScript变量中使用$
并不是真正必要的,也不是建议的。我之所以在这里使用它,是因为它是我目前正在使用的JS样式指南的一部分。你为什么认为不建议这样做@Tadmannat这是一个非常奇怪的风格指南。