有没有办法使用字符串数组作为变量名并进行解构赋值? (Javascript)

问题描述 投票:0回答:1

让我们记住,通常的解构赋值是如何处理数组的:

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'

我觉得它不是为这种情况设计的,对吗?

javascript arrays destructuring
1个回答
0
投票

您无法在没有索引的情况下访问数组,但您可以使用 for 循环来使用它,然后检查条件

© www.soinside.com 2019 - 2024. All rights reserved.