If I draw from the object y, for example 'JPY 20, USD 20', I want to replace the USD and JPY abbreviation with the extended name DOLLAR and JEN. It has to look like this: 'JPY 20, USD 20' replace to 'JEN 20, DOLLAR 20'
var x = {
'U': 'DOLLAR',
'J': 'JEN',
'E': 'EURO'
};
var y = [
'U 50',
'J 20, U 20',
'E 20, J 10'
];
var z = y[Math.floor(Math.random() * y.length)]; //example 'JPY 20, USD 20'
for(var key in x) {
var c = new RegExp({key}, "g");
z = z.replace(key, x[key]);
console.log(z); //It should looks like //'JEN 20, DOLLAR 20'
}
var x = { U: 'DOLLAR', J: 'JEN', E: 'EURO' },
y = ['U 50', 'J 20, U 20', 'E 20, J 10'],
z = y[Math.floor(Math.random() * y.length)],
key,
regex;
for (key in x) {
regex = new RegExp(key, "g");
z = z.replace(regex, x[key]);
}
console.log(z);