Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert array values into strings in javascript [closed]

I have this array [ABC, QWE, XYZ] I would like to turn it into ['ABC', 'QWE', 'XYZ']

When I try to manipulate values in the current array I get: ReferenceError: ABC is not defined

Any ideas on how should I do it?

Thanks!

like image 943
Quentin Del Avatar asked Sep 11 '26 15:09

Quentin Del


2 Answers

Convert arrays element types: Number to strings

    var strArr = [1,2,3,4,5].map(String);
    // Result: ["1","2","3","4","5"]
like image 145
Grigor Ghazaryan Avatar answered Sep 13 '26 04:09

Grigor Ghazaryan


We can't do that directly but after little bit change you can do that... So the current array you said like array [ABC, QWE, XYZ],

Lets design you keys in object first:

var obj = {
ABC:1, QWE:'somevalue', XYZ:new Date()
}

So I created object obj having your variables lets say the three variables, now lets convert:

var arr = [];

for (var key in obj){
    console.log(key, obj[key]);
    arr.push(String(key));
}

console.log(arr);// you will see the desire result.

Running example here : example

like image 41
sujeet kumar srivastava Avatar answered Sep 13 '26 04:09

sujeet kumar srivastava