I have the following problem:
I have the following JSON:
var words = {
"categorias": [
{
"Licores": ["Smirnoff", "Johnnie Walker", "Bacardi", "Martini", "Hennessy", "Absolut", "Jack Daniels", "Chivas Regal", " Baileys", "Ballantines", "CAPTAIN MORGAN", "CUERVO", "JAEGERMEISTER", "MOET ET CHANDON", "DEWARS", "JIM BEAM", "GALLO", "HARDYS", "CROWN ROYAL", "RICARD", "CONCHA Y TORO", "GREY GOOSE", "GORDONS", "GRANTS", "JAMESON", "MALIBU", "STOLICHNAYA", "MARTELL", "HAVANA CLUB", "REMY MARTIN", "PATRON", "YELLOWTAIL", "SAUZA", "SKYY", "FINLANDIA", "BERINGER", "TANQUERAY", "DREHER", "BEEFEATER", "BOMBAY", "SEAGRAM", "CANADIAN CLUB", "GLENFIDDICH", "COINTREAU", "TEACHERS", "KAHLUA", "BELLS", "CINZANO VERMOUTH", "LINDEMANS", "COURVOISIER", "CANADIAN MIST", "TORRES", "INGLENOOK", "CASTILLO", "KUMALA", "PENFOLDS", "LANSON", "Ron", "Vodka", "Whisky", "Bourbon", "Brandy", "Cognac", "Tequila", "Ginebra", "Vino blanco", "Vino tinto", "Champagne", "Cerveza", "Budweiser", "Heineken", "Sambuca", "Frangelico", "Triple Sec", "Licor de cafe", "Kirsch", "Fernet", "Aguardiente", "Pisco", "Sangría", "Mojito", "Margarita", "Cuba libre", "Daiquiri", "Cosmopolitan", "Caipirinha", "White Russian", "Coco Loco", "Mai Tai", "Manhattan", "Zombie", "Gintonic", "Hurricane", "Negroni", "Paloma", "Farnell"]
},
{
"animales": ["Abadejo", "Abanto", "Abeja", "Abeja doméstica", "Abejorro", "Abubilla", "Abulón", "Acedía", "Acentor", "Acevia", "Acocil", "Acranio", "Actinia", "Addax", "Agachadiza", "Aguará", "Águila", "Agutí", "Ajolote", "Alacrán", "Albatros", "Alburno", "Alcaraván", "Alcatraz", "Alcaudón", "Alce", "Alcélafo", "Alimoche", "Almeja", "Alondra ibis", "Alosa", "Alpaca", "Alzacola", "Ameba", "Ampelis", "Anaconda", "Anchoa", "Anfioxo", "Angelote", "Anguila", "Aninga", "Anoa", "Anolis", "Ánsar", "Anta", "Antílope", "Araguato", "Araña", "Arapaima", "Arapapa", "Ardilla", "Arenque", "Argonauta", "Armadillo", "Armiño", "Arrendajo", "Asno", "Atún", "Avefría", "Avestruz", "Avispa", "Avetoro", "Avispón", "Avoceta", "Avutarda", "Ayeaye", "Ayu", "Babirusa", "Babosa", "Babuino", "Bacalao", "Baiji"]
}
]
}
So when I want search a Categoria
I call this function:
function random(max){
return Math.floor((Math.random() * max) + 0);
}
Now as the Key of JSON is a array (categorias), and this key i want get of way random, then i use Object.keys(), and it runs smoothly, when i assigned the var namKeyJson to Object.keys(words.categorias[randomCategory]), i returned the name of the key which i need
var lengthCategory = words.categorias.length-1;
var randomCategory = random(lengthCategory);
var nameKeyJson = Object.keys(words.categorias[randomCategory]);
nameKeyJson = nameKeyJson.toString();
the problem is when i want get the values or the length the categorys of array, and that by placing the variable nameKeyJson, takes the name of the variable but not its value.
var lengthPregunta = words.categorias[randomCategory].nameKeyJson.length;
console.log(lengthPregunta);
Thanks for help me.