I'm trying to create objects from a key/value list. My Problem is, that Object Properties have to be valid JavaScript identifiers. At least with Adobe ExtendScript I can perfectly create an Object with a wrong Property (See Example: wrong-key -> "-"-Literal is invalid).
var kvp = ["key;value", "wrong-key;value"];
var obj = {};
for (var i = 0 ; i < kvp.length; i++) {
pair = kvp[i].split(";");
obj[pair[0]] = pair[1];
}
alert (obj.key);
alert (obj.wrong-key); // -> Throws an Error
Of course I could run a replace(/-/,"_")
, but is there any encoding/escaping function out there to accomplish this goal more generally?
thanks, gregor
you can access those properties with special chars using the array notation
console.log(obj["wrong-key"]);