I have the following code where I need to get a random value:
const namesJson = {
"nombre1": "Raghat",
"nombre2": "Uhmla",
"nombre3": "Borto"
};
const btnName = document.getElementById('btn')
btnName.onclick = function names() {
var properties = Object.getOwnPropertyNames(namesJson);
var index = Math.floor(Math.random() * properties.length);
var output = {};
output[properties[index]] = namesJson[properties[index]];
document.getElementById("mensaje").innerHTML = JSON.stringify(output);
}
<button type="button" id="btn">Get random name</button>
<h2>Mensaje:</h2>
<div id="mensaje"></div>
With this I have {"nombre2": "Uhmla"}
I want to get the value like Uhmla
, how can I do?
Comments
Post a Comment