将变量名数组转换为字符串js

时间:2018-03-26 22:03:50

标签: javascript

var a = "aa", b= "bb",c ="cc", d="dd", e="ee";
array = [a,b,c,d,e] // outputs ["aa", "bb", "cc", "dd", "ee"];

然而,javascript中是否有可能将变量(a,b,c,d,e)转换为字符串?

赞:" a"," b"," c"," d"," e"? ?

P.S:数组值也可以是动态的,也可以是上面提到的长度。

感谢您的帮助!!

2 个答案:

答案 0 :(得分:2)

您可以使用ES6速记属性名称并返回字符串数组。



let a = "aa", b= "bb",c ="cc", d="dd", e="ee";
let strings = Object.keys({a, b, c, d, e});
console.log(...strings)




答案 1 :(得分:0)

像这样的东西

var a = "aa", b= "bb",c ="cc", d="dd", e="ee";
var array = [a,b,c,d,e];
({a,b,c,d,e} = array)
var keys = Object.keys({a,b,c,d,e});
console.log(keys)
console.log(array)