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

However is there a possibility in javascript to convert the variables (a, b,c,d,e) into strings?

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

P.S: the array values could be dynamic as well or more than the length mentioned above.

Thanks for the help!!

user1234
  • 3,000
  • 4
  • 50
  • 102

2 Answers2

2

You could do this with ES6 shorthand property names and return array of strings.

let a = "aa", b= "bb",c ="cc", d="dd", e="ee";
let strings = Object.keys({a, b, c, d, e});
console.log(...strings)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
  • that would ideally work if there was a fixed length of values in that array. My array could have 10 -20 items in it. Object.keys(a,b,c,....) might make it cumberosme to add every variable value to convert it to string..? – user1234 Mar 26 '18 at 22:18
  • Why not just use object instead of an array, then you have key and value? – Nenad Vracar Mar 26 '18 at 22:25
  • I tried this: Object.assign({}, array);, //outputs to: {0: "aa", 1: "bb", 2: "cc", 3: "dd", 4: "ee"} it will still give me the values and not the variable names. – user1234 Mar 26 '18 at 22:40
0

Something like this

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)
vinayakj
  • 5,591
  • 3
  • 28
  • 48
  • sorry that may not work...same thing I mentioned above for @Nenad's answer. ({a,b,c,d,e} ) are just examples, the values could be more than 10 or 50, in this case ({a,b,c,d,e,......)} might not be faesible. – user1234 Mar 26 '18 at 22:30
  • maybe If you explain the complete use case, we could help, like why do you need to do this, how its being used – vinayakj Mar 27 '18 at 04:20