首页 > 解决方案 > 有没有办法将字符串数组用作具有解构赋值的变量名?(Javascript)

问题描述

让我们记住,通常的解构赋值如何与数组一起工作:

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'

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

标签: javascriptarraysdestructuring

解决方案


推荐阅读