让我们记住,通常的解构赋值是如何处理数组的:
const numbers = ['one', 'two', 'three'];
[one, two, three] = numbers;
console.log(`${one} ${two} ${three}`);
但是如果我们有很长的数据数组(例如 20 个项目)怎么办?像上面的例子那样手动列出它们似乎不太好。有没有办法通过解构赋值提取字符串数组并将字符串用作值?
伪代码:
let numbers = ['one', 'two', 'three'];
[...numbers] = numbers;
预期结果:
console.log(one); // 'one'
console.log(two); // 'two'
console.log(three); // 'three'
我觉得它不是为这种情况设计的,对吗?
您无法在没有索引的情况下访问数组,但您可以使用 for 循环来使用它,然后检查条件